源代码:
1.压缩文件:
package javaio;
import java.io.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.*;
public class MyZip {
private void zip(String zipFileName,File inputFile)throws Exception{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out,inputFile,"");
System.out.println("压缩中......");
out.close();
}
private void zip(ZipOutputStream out,File f,String base)throws Exception{
if(f.isDirectory()) {
File[] fl = f.listFiles();
if(base.length()!=0) {
out.putNextEntry(new ZipEntry(base +"/"));
}
for(int i = 0 ; i<base.length();i++) {
zip(out,fl[i],base+fl[i]);
}
}
else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b ;
System.out.println("base");
while((b=in.read())!=-1) {
out.write(b);
}
in.close();
}
}
public static void main(String[] temp) {
MyZip book = new MyZip();
try {
book.zip("F:/mr.zip", new File("F:/test/"));
System.out.println("压缩完成");
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
2.解压文件:
package javaio;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class Decompressing {
public static void main(String[] temp) {
File file = new File("F:/mr.zip");
ZipInputStream zin;
try {
ZipFile zipFile = new ZipFile(file);
zin = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = zin.getNextEntry();
while(((entry = zin.getNextEntry())!=null) && !entry.isDirectory()){
File tmp = new File("D:/"+entry.getName());
if(!tmp.exists()) {
tmp.getParentFile().mkdirs();
OutputStream os = new FileOutputStream(tmp);
InputStream in = zipFile.getInputStream(entry);
int count = 0;
while((count = in.read())!=-1) {
os.write(count);
}
os.close();
in.close();
}
zin.closeEntry();
System.out.println(entry.getName()+"解压成功");
}
zin.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
可滚动的表格:
package javaswing;
import javax.swing.*;
import java.awt.*;
public class JTable_JScrollPane_test extends JFrame{
public static void main(String args[]) {
JTable_JScrollPane_test frame = new JTable_JScrollPane_test();
frame.setVisible(true);
}
public JTable_JScrollPane_test() {
super();
setTitle("创建可以滚动的表格");
setBounds(100,100,240,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"A","B"};
String[][] tableValues ={ {"A1","B1"},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"},{"A6","B6"}};
JTable table = new JTable(tableValues,columnNames);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane,BorderLayout.CENTER);
}
}
运行截图:
学习总结:
Swing 高级组件
Swing 还提供了一些高级组件,如分割面板、选项卡面板、菜单、工具栏和文件选择器,以及进度条、表格等,还有为程序添加快捷操作等。
一、高级面板组件
1、JSplitPane 分割面板
分割面板由 javax.swing.JSplitPane 类实现,用来将其所在的区域分割成两部分,可以根据需要决定是水平分割还是垂直分割。
两部分之间存在一个分隔条,通过调整分隔条的位置,可以改变这两部分的相对大小。
JSplitPane 类的常用构造方法如下表:
构造方法 |说明
--|:--😐--:
JSplitPane()| 创建一个默认的分割面板;默认情况下会在水平方向上进行分割,重绘方式只在调整分隔条位置完成时重绘。
JSplitPane(int newOrientation)| 创建一个按照指定方向分割的分割面板。入口参数 newOrientation 的可选静态常量有 HORIZONTAL_SPLIT(在水平方向分割)和 VERTICAL_SPLIT (在垂直方向分割)
JSplitPane(int newOrientation,boolean newContinuousLayout)| 创建一个按照指定方向分割,并且按照指定方式重绘分割面板。如果将入口参数newContinuousLayout 设置为 true,表示在调整分隔条位置的过程中连续重绘,设为 false 则表示只在调整分割条位置完成时重绘。
JSplitPane 类的常用方法如下表:
方法| 说明
--|:--😐--:
setOrientation(int orientation) |设置面板的分割方向,即水平分割(默认)还是垂直分割
setDividerLocation(int location)| 设置分隔条的绝对位置,即分隔条左侧(水平分割)的宽度或上方(垂直分割)的高度
setDividerLocation(double proportionalLocation)| 设置分隔条的相对位置,即分隔条左侧或上方的大小与分割面板大小的百分比
setDividerSize(int newSize) |设置分隔条的宽度,默认为 5 像素
setLeftComponent(Component comp)| 将组件设置到分隔条的左侧或上方
setTopComponent(Component comp) |将组件设置到分隔条的上方或左侧
setRightComponent(Component comp)| 将组件设置到分隔条的右侧或下方
setBottomComponent(Component comp)| 将组件设置到分隔条的下方或右侧
setOneTouchExpandable(boolean newValue)| 设置分割面板是否提供 UI 小部件。设置为 true 表示提供,有些外观不支持该功能,这时将忽略该设置;设为 false 表示不提供,默认为不提供
setContinousLayout(boolean newContinuousLayout) |设置调整分隔条位置时面板的重绘方式。设为 true 表示在调整过程中连续重绘,设为 false 则表示只在调整完成时重绘
作者:xudo~
出处:https://www.cnblogs.com/xudo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具