------- android培训、java培训、期待与您交流! ----------
Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中
java.Awt(Abstract Window ToolKit)(抽象窗口工具包),需要调用本地系统方法实现功能。重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,轻量级控件。
布局管理器:容器中组件的排列方式
常见的布局管理器:FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout
创建图形化界面:
1. 创建frame窗体
2. 对窗体进行基本设置
3. 定义组件
4. 将组件通过窗体的add方法添加到窗体中
5. 让窗体显示,通过setVisible(true)
事件监听机制的特点:
事件源:就是awt包或者swing包中的那些图形界面组件
事件:每一个事件源都有自己所特有的对应事件和共性事件
监听器:将可以触发某一个事件的动作封装到监听器中
事件处理:对产生的动作进行处理
适配器:抽象类,复写监听器的方法,但是方法为空,目的是方便创建监听器对象
如:WindowListener的子类WindowAdapter已经实现了WindowListener接口,并覆盖了其中所有方法,因此只需基础WindowAdapter覆盖所需要的方法即可。
练习1. 简单窗体程序,显示出指定路径下的文件名称
public class MyWindowDemo {
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyWindowDemo() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("goooooo");
ta = new TextArea(25, 70);
d = new Dialog(f, "提示信息-self", true);
d.setBounds(400, 200, 260, 180);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
okBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
}
});
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showDir();
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void showDir() {
String dirPath = tf.getText();
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
ta.setText("");
String[] names = dir.list();
for (String name : names) {
ta.append(name + "\r\n");
}
} else {
String info = "您输入的路径:" + dirPath + "是错误的";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args) {
new MyWindowDemo();
}
}
练习2. 简易记事本程序(包含打开文件,保存文件,退出功能)
public class MyMenuTest {
private Frame f;
private MenuBar mb;
private Menu fileMenu;
private TextArea ta;
private MenuItem closeItem, openItem, saveItem;
private FileDialog openDia, saveDia;
private File file;
MyMenuTest() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300, 100, 500, 600);
// f.setLayout(new FlowLayout());
mb = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
openItem = new MenuItem("open");
saveItem = new MenuItem("save");
closeItem = new MenuItem("exit");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
mb.add(fileMenu);
f.setMenuBar(mb);
openDia = new FileDialog(f, "open", FileDialog.LOAD);
saveDia = new FileDialog(f, "save", FileDialog.SAVE);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
// 打开操作
openItem.addActionListener(new ActionListener() {
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);
// 利用缓存流将数据写入文本区域
try {
BufferedReader bufr = new BufferedReader(new FileReader(
file));
String line = null;
while ((line = bufr.readLine()) != null) {
ta.append(line + "\r\n");
}
bufr.close();
} catch (IOException e1) {
System.out.println("Mission Failed");
}
}
});
// 保存操作
saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 先判断文件名是否存在
if (file == null) {
openDia.setVisible(true);
// 获取文件路径和名称
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
// 判断;路径或文件名为空则返回
if (dirPath == null || fileName == null)
return;
file = new File(dirPath, fileName);
}
// 利用缓存流写入文本区域的内容
try {
BufferedWriter bufw = new BufferedWriter(new FileWriter(
file));
String text = ta.getText();
bufw.write(text);
bufw.close();
} catch (IOException e1) {
throw new RuntimeException();
}
}
});
closeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new MyMenuTest();
}
}
如何制作可以双击执行的jar包呢?
1,将多个类封装到了一个包(package)中。
2,定义一个jar包的配置信息。
定义一个文件a.txt 。文件内容内容为:
Main-Class:(空格)包名.类名(回车)
3,打jar包。
jar -cvfm my.jar a.txt 包名
4,通过winrar程序进行验证,查看该jar的配置文件中是否有自定义的配置信息。
5,通过工具--文件夹选项--文件类型--jar类型文件,通过高级,定义该jar类型文件的打开动作的关联程序。
jdk\bin\javaw.exe -jar