java.awt:AbstractWindow Toolkit(抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
java.swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全有java实现,曾强了移植性,属轻量级控件。
常见的布局管理器:
FlowLayout:流式布局管理器,从左到右的顺序排列,Panel的默认布局管理器
BorderLayout:边界布局管理器,东南西北中,Frame的默认布局管理器
GridLayout:网格布局管理器,规则矩阵
CardLayout:卡片布局管理器,选项卡
GridBagLayout:网格包布局管理器,非规则矩阵
创建图形化界面步骤:
1. 创建frame窗体
2. 对窗体进行基本设置,比如大小,位置,布局
3. 定义组件
4. 将组件通过窗体的add方法添加到窗体中
5. 让窗体显示,通过setVisible(true)
package mypackage.gui;
import java.awt.*;
import java.awt.event.*;
import static system.out.Sop.*;
public class FrameDemo {
private Frame f;
private Button btn;
public FrameDemo() {
init();
}
private void init() {
f = new Frame("my frame");
// 对窗体进行基本设置
f.setBounds(300, 200, 500, 400);
f.setLayout(new FlowLayout());
btn = new Button("退出");
f.add(btn);
// 加载窗体时间
myEvent();
f.setVisible(true);
}
private void myEvent() {
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sop("退出,按钮干的");
System.exit(0);
}
});
}
public static void main(String[] args) {
new FrameDemo();
}
}
菜单示例和打开文件
package mypackage.gui;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuDemo {
private Frame f;
private MenuBar bar;
private Menu fileMenu, subMenu;
private MenuItem closeItem, subItem, openItem, saveItem;
private FileDialog openDia, saveDia;
private TextArea ta;
File file=null;
public MyMenuDemo() {
init();
}
private void init() {
f = new Frame("my menuDemo");
f.setLayout(new BorderLayout());
f.setBounds(200, 50, 750, 650);
bar = new MenuBar();
fileMenu = new Menu("文件");
subMenu = new Menu("子菜单");
closeItem = new MenuItem("退出");
subItem = new MenuItem("子项");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
bar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(subMenu);
fileMenu.add(closeItem);
subMenu.add(subItem);
f.setMenuBar(bar);
openDia = new FileDialog(f, "打开文件", FileDialog.LOAD);
saveDia = new FileDialog(f, "保存文件", FileDialog.SAVE);
ta = new TextArea();
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
closeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
if (dirPath == null || fileName == null)
return;
ta.setText("");
file = new File(dirPath, fileName);
BufferedReader bufr = null;
try {
bufr = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bufr.readLine()) != null) {
ta.append(line + "\r\n");
}
} catch (FileNotFoundException e1) {
throw new RuntimeException("找不到该文件");
} catch (IOException e1) {
throw new RuntimeException("读取异常");
} finally {
if (bufr != null)
try {
bufr.close();
} catch (IOException e1) {
throw new RuntimeException("关闭流失败");
}
}
}
});
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (file==null||!file.exists()) {
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if (dirPath == null || fileName == null)
return;
file = new File(dirPath, fileName);
}
BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new FileWriter(file));
bufw.write(ta.getText());
} catch (IOException e1) {
throw new RuntimeException("IO异常");
} finally {
if (bufw != null)
try {
bufw.close();
} catch (IOException e1) {
throw new RuntimeException("关闭流失败");
}
}
}
});
}
public static void main(String[] args) {
new MyMenuDemo();
}
}
Jar包双击执行:
1. 将程序放入一个包中如mypackage
2. 编译到指定目录下,javac –d c:\myclass Test.java
3. 进入到c:\myclass目录下,建立一个配置文件,比如1.txt,写入信息Main-Class:mypackage.Test。
4. 在doc中进入c:\myclass目录下,执行命令语句:
jar cvfm my.jar 1.txt mypackage