一、总结
1、写清注释后再写程序
2、程序模块化
3、多参考别人的程序,理解别人的思想,再结合自己的想法能学到更多的东西
4、基本功能实现后,再调整用户体验
二、JFrame
private void cleanActionPerformed(java.awt.event.ActionEvent evt) {
Login log = new Login(name, secret, warn);
log.clean();
}
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
Login log = new Login(name, secret, warn);
if(log.login()){
//隐藏当前界面
this.setVisible(false);
//计时线程
new Thread(){
public void run(){
//线程退出标志
flag = false;
//创建主界面
MainFrame main = new MainFrame("欢迎:"+name.getText());
//界面居中
FrameUtil.centerShowJFrame(main);
main.setVisible(true);
//获取当前时间,将时间传给主界面
while(true){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
main.setTime1(time);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(flag){
break;
}
}
}
}.start();
}else{
JOptionPane.showMessageDialog(this, "对不起,用户名或密码错误");
}
}
注:Swing传参方式有两种:构造方法传参、set\get方法传参
三、Swing弹出框简介
1、弹出框的样式一共有四种:
ConfirmDialog:确认对话框(有类似Yes,No等按钮的那种).你也可以传不同的参数进去来改变显示的按钮和icon
MessageDialog:显示消息
InputDialog:文本输入对话框
OptionDialog:自定义对话框(组合以上三种对话框的类型)
2、方法参数如下:
ParentComponent:对话框的父窗口对象。如果为null则采用缺省的Frame作为父窗口,此时对话框将设置在屏幕的正中。
message:显示的消息文字.其实可以是任何的对象.(我今天无意中把窗体上的JMenuBar对像传进去它显示的就是JMenuBar)
title:标题文字
Component:中显示的组件(如按钮)
Icon:显示的图标(如感叹号)
messageType:一般可以为如下的值ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE、PLAIN_MESSAGE
optionType:它决定在对话框的底部所要显示的按钮选项。一般可以为DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTION
四、屏幕居中
public class FrameUtil {
public static void centerShowJFrame(Component component) {
//获得屏幕的大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//获得窗口的大小
Dimension frameSize = component.getSize();
//如果窗口的大小比屏幕的要大,就用屏幕的赋值给窗口的。
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
//设置窗口的显示位置居中。
component.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
//显示窗口
component.setVisible(true);
}
}
五、压缩、解压缩
public class ZipDao {
public static void zipFile(File inFile, ZipOutputStream zos, String dir)
throws IOException {
if (inFile.isDirectory()) {
File[] files = inFile.listFiles();
for (File file : files) {
//如果是目录就递归的压缩
zipFile(file, zos, dir + "\\" + inFile.getName());
}
} else {
String entryName = null;
if (dir!=null&&!dir.equals("")) {
entryName = dir + "\\" + inFile.getName();
} else {
entryName = inFile.getName();
}
//把文件封装成为ZipEntry对象
ZipEntry entry = new ZipEntry(entryName);
//添加到ZipOutputStream对象中
zos.putNextEntry(entry);
InputStream is = new FileInputStream(inFile);
int len = 0;
while ((len = is.read()) != -1) {
zos.write(len);
}
is.close();
}
}
public static void unZipFile(String zipFileName, String targetDir) {
try {
File file = new File(zipFileName);
// 实例化ZipFile,每一个zip压缩文件都可以表示为一个ZipFile
ZipFile zipFile = new ZipFile(file);
// 实例化一个Zip压缩文件的ZipInputStream对象
ZipInputStream zipInputStream = new ZipInputStream(
new FileInputStream(file));
ZipEntry zipEntry = null;
// getNextEntry()方法依次拿到每一个ZipEntry对象
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String fileName = zipEntry.getName();
File temp = new File(targetDir + fileName);
if (!temp.getParentFile().exists()) {
temp.getParentFile().mkdirs();
}
OutputStream os = new FileOutputStream(temp);
// 通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
InputStream is = zipFile.getInputStream(zipEntry);
int len = 0;
while ((len = is.read()) != -1) {
os.write(len);
}
os.close();
is.close();
}
zipInputStream.close();
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
/* File inFile = new File("D:\\abc");
ZipOutputStream zos;
try {
zos = new ZipOutputStream(new FileOutputStream("D:\\abc.zip"));
zipFile(inFile, zos, "");
zos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
unZipFile("D:\\abc.zip","c:\\");
}
}