第8章 GUI
8.1 概述
1、GUI
GraphicalUser Interface(图形用户接口)。
用图形的方式,来显示计算机操作的界面,这样更方便更直观。
2、CLI
Command line User Interface (命令行用户接口)
就是常见的Dos命令行操作。
需要记忆一些常用的命令,操作不直观。
3、Awt和Swing
Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中
java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。
属重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java
实现。增强了移植性,属轻量级控件。
4、布局管理器
容器中的组件的排放方式,就是布局。常见的布局管理器:
⑴、FlowLayout(流式布局管理器):从左到右的顺序排列。
⑵、BorderLayout(边界布局管理器):东,南,西,北,中
⑶、GridLayout(网格布局管理器):规则的矩阵
⑷、CardLayout(卡片布局管理器)
⑸、GridBagLayout(网格包布局管理器)
8.2 JFrame演示
1、Jframe顶层窗口的层次示意图
2、构造方法
public JFrame():构造一个最初不可见的 Frame
新实例(),JFrame
的标题为空.
public JFrame(String title):构造一个新的、最初不可见的、具有指定标题的 JFrame
对象
4、应用程序界面
⑴、在Jframe中添加组件
①、建窗体,并做基本设置。 比如大小,位置,布局。
②、建立一个与Jframe相关联的内容面板(contentPane),内容面板其实就是一个中间容器
③、建立起组件,并将组件通过窗体的add方法添加到面板中。
④、体显示让窗体显示
代码示例
publicclass HelloSwing {
staticfinalintWIDTH =300;
staticfinalintHEIGHT =200;
publicstaticvoid main(String[] args){
JFramejf = new JFrame("helloSwing");
jf.setSize(WIDTH, HEIGHT);
//使窗口上的最大化、最小化以及关闭键发挥作用
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
//创建中间容器,面板容器类
JPanelcontentPane = new JPanel();
//将中间容器依托在顶层容器内
jf.setContentPane(contentPane);
JButtonbutton = new JButton("OK");
contentPane.add(button);
}
}
⑵、在Jframe中添加菜单栏
publicclass AddMenu {
publicstaticvoidmain(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("学生管理系统");
//添加菜单组件
JMenuBar menuBar = new JMenuBar();
//将菜单加入顶层容器中
frame.setJMenuBar(menuBar);
//设置菜单组件
JMenu menu1 = new JMenu("文件");
JMenu menu2 = new JMenu("编辑");
JMenu menu3 = new JMenu("视图");
//将菜单组件添加到菜单条组件中
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
//创建菜单项组件
JMenuItem item1 = new JMenuItem("打开");
JMenuItem item2 = new JMenuItem("保存");
JMenuItem item3 = new JMenuItem("打印");
JMenuItem item4 = new JMenuItem("退出");
//将菜单项组件添加到相应的菜单组件中
menu1.add(item1);
menu1.add(item2);
menu1.addSeparator();
menu1.add(item3);
menu1.addSeparator();
menu1.add(item4);
frame.setVisible(true);
}
}
8.3 事件监听机制
1、组成
⑴、事件源(组件):就是awt包或者swing包中的那些图形界面组件。
⑵、事件(Event):用户对组件发生的动作,每一个事件源都有自己特有的对应事件和共性事件。
⑶、监听器(Listener):系统接收到所产生的事件,然后根据这些事件,做相应的处理。
以上三者,在java中都已经定义好了。直接获取其对象来用就可以了。我们要做的事情是,就是对产生的动作进行处理。
⑷、事件处理(引发事件后处理方式)
2、流程图
3、Swing监听器
⑴、事件处理的过程与步骤
①、定义实现事件监听接口类
②、创建事件监听器
③、像事件源注册监听器对象
publicclass EventHandle {
staticJPanel contentPane;
staticJTextField textField=new JTextField(15);
publicstaticvoidmain(String[] args) {
JFrame frame = new JFrame("测试程序");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(0, 2, 0, 2));
contentPane.setLayout(new BorderLayout(0, 0));
frame.setContentPane(contentPane);
contentPane.add(textField, BorderLayout.NORTH);
JButton btnNewButton = new JButton("清空文本框中的信息");
contentPane.add(btnNewButton, BorderLayout.SOUTH);
//以下为处理事件代码
//创建事件监听器
ActionListener al = new ActionHandle();
//向事件源注册监听对象
btnNewButton.addActionListener(al);
}
}
//定义实现事件监听接口类,
class ActionHandle implements ActionListener {
@Override
publicvoidactionPerformed(ActionEvent e) {
new EventHandle().textField.setText("");
}
}
⑵、匿名类方式处理事件
上面的程序太过臃肿,事件处理越多,可读性就越差,所以用匿名类的方式处理事件不失为一种好方法。下面是对上面事件处理的部分的代码的优化:
btnNewButton.addActionListener(new ActionListener() {
publicvoidactionPerformed(ActionEvent e) {
new EventHandle().textField.setText("");
}
});
⑶、适配器
在事件监听接口中有很多方法,但程序员一般不需要实现每一个方法,但接口有个规定,即要实现接口,就必须实现接口中的每个方法。针对这种情况,适配器类应运而生。
⑷、Swing所支持的事件监听器
⑸、窗口事件的处理
publicclass WindowListenerDemo extends JFrame {
privateJPanel contentPane;
publicstaticvoidmain(String[] args) {
EventQueue.invokeLater(new Runnable() {
publicvoidrun() {
try {
WindowListenerDemo frame = new WindowListenerDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
publicWindowListenerDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
super.setTitle("测试窗口");
WindowListener wl=new Windowhandler();
addWindowListener(wl);
}
classWindowhandler implements WindowListener{
// 用户试图从窗口的系统菜单中关闭窗口时调用。
publicvoidwindowClosing(WindowEvent e) {
JButton ok = new JButton("确定");
JButton cancel = new JButton("取消");
JLabel l =new JLabel("你能确定关闭系统了吗");
JDialog d = new JDialog((JFrame)e.getSource(),"系统出错了!",true);
d.setSize(200, 100);
d.setLocation(0, 0);
contentPane.setLayout(new GridLayout(1,2));
d.add(contentPane, "South");
d.add(l,"Center");
contentPane.add(ok);
contentPane.add(cancel);
d.setVisible(true);
ok.setVisible(true);
cancel.setVisible(true);
l.setVisible(true);
}
//窗口被打开时调用的方法
publicvoidwindowOpened(WindowEvent e) {}
//因对窗口调用 dispose 而将其关闭时调用。
publicvoidwindowClosed(WindowEvent e) {}
//窗口从正常状态变为最小化状态时调用。
publicvoidwindowIconified(WindowEvent e) {}
// 窗口从最小化状态变为正常状态时调用。
publicvoidwindowDeiconified(WindowEvent e) {}
//将 Window 设置为活动 Window 时调用
publicvoidwindowActivated(WindowEvent e) {}
//当 Window 不再是活动 Window 时调用。
publicvoidwindowDeactivated(WindowEvent e) {}
}
}
⑹、动作事件的处理
动作事件主要针对组件,如单击按钮等,其监听接口是ActionListener接口。
publicclass ActionListenerDemo extends Frame{
JButton b;
publicActionListenerDemo(String str){
super(str);
b = new JButton("确认");
add(b);
b.addActionListener(new ActionListener(){
publicvoidactionPerformed(ActionEvent e) {
//getSource():返回最初发生 Event 的对象。
((JButton)e.getSource()).setLabel("取消");
}});
}
publicstaticvoidmain(String[] args){
ActionListenerDemo ald = new ActionListenerDemo("动作事件测试");
// 调整此窗口的大小,以适合其子组件的首选大小和布局。
ald.pack();
ald.setVisible(true);
}
}
⑺、焦点事件的处理
如果在用户程序界面上有多个组件,,但每次也只能操作一个组件,也就是说每次操作的焦点只能停在一个组件上。它有两种接口focusGained和focusLost