1 / 22

第八章 Swing 图形用户界面程序设计

第八章 Swing 图形用户界面程序设计. 浙江工业大学 计算机学院 赵小敏 zxm@zjut.edu.cn http://210.32.200.159/java2010. 重点要掌握的内容. 重点: 组件、容器、布局管理器等概念。 了解 GUI 组件包的类层次结构、 事件处理模型,掌握事件源、事件、事件处理者等概念. 重点要掌握的内容 ( 续 ). 熟悉编写 GUI 程序基本步骤,学会编写 GUI 程序 引入用到组件和事件的包或类 声明类并实现事件的接口 声明界面中需用到的组件

ratana
Télécharger la présentation

第八章 Swing 图形用户界面程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第八章 Swing图形用户界面程序设计 浙江工业大学 计算机学院 赵小敏 zxm@zjut.edu.cn http://210.32.200.159/java2010

  2. 重点要掌握的内容 • 重点:组件、容器、布局管理器等概念。 • 了解GUI组件包的类层次结构、事件处理模型,掌握事件源、事件、事件处理者等概念

  3. 重点要掌握的内容(续) • 熟悉编写GUI程序基本步骤,学会编写GUI程序 • 引入用到组件和事件的包或类 • 声明类并实现事件的接口 • 声明界面中需用到的组件 • 在构造方法中创建组件的对象,对界面进行合理布局,并注册相关组件的事件监听器,最后设置界面大小并显示界面 • 实现接口的方法(事件的处理方法) • 写main方法进行测试

  4. 重点要掌握的内容(续) • 熟悉常见容器:JFrame,JPanel,JScrollPane, Container • 熟悉常见基本组件: JButton, JLabel, JComboBox, JList,JTextField, JTextArea, JCheckBox,JRadioButton • 熟悉常见的布局管理器: FlowLayout, BorderLayout, BoxLayout, GridLayout • 熟悉事件处理的机制,掌握常见组件如按钮JButton、文本域JTextField、下拉选项JComboBox 、复选框JCheckBox 等的事件处理方法

  5. Java的事件处理机制 包含事件处理的程序应该包括以下四部分内容: 1、引入系统事件类包,如import java.awt.event.*。 2、在事件处理类的声明中指定要实现的监听器名,如: public class MyClass implements ActionListener { …} 3、注册事件源对象的事件监听者,如btn.addActionListener (this)。 4、实现监听器中的接口 如实现按钮事件监听接口ActionListener : public void actionPerformed(ActionEvent e) { ...//响应某个动作的代码... }

  6. 如何编写GUI程序? • 例:编写一个允许学生在文本字段中输入一个数的程序。创建一个每当用户单击一次就将此数加一的按钮。创建另一个每当用户单击一次就将此数减一的按钮。 界面效果如下图所示。

  7. 引入用到组件和事件的包或类 声明类并实现事件的接口 • import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • class Incrementor implements ActionListener{ • JTextField numberTxf; • JButton incrementBtn,decrementBtn; • public void makeGUI(){ • JFrame frm = new JFrame("Incrementor"); • Container c=frm.getContentPane(); • c.setLayout(new FlowLayout()); • numberTxf = new JTextField("0",5); • c.add(numberTxf); • incrementBtn = new JButton("Increment"); • c.add(incrementBtn); • incrementBtn.addActionListener(this); • decrementBtn= new JButton("Decrement"); • c.add(decrementBtn); • decrementBtn.addActionListener(this); • frm.setSize(300,100); • frm.setVisible(true); • } 声明界面中需用到的组件 在构造方法中创建组件的对象,对界面进行合理布局,并注册相关组件的事件监听器,最后设置界面大小并显示界面

  8. public void actionPerformed(ActionEvent e) { • int oldNum = Integer.parseInt(numberTxf.getText()); • int newNum = oldNum; • if(e.getSource()==incrementBtn){ • newNum++; • } • else if(e.getSource()==decrementBtn){ • newNum--; • } • numberTxf.setText(String.valueOf(newNum)); • } • public static void main(String args[]) { • Incrementor i = new Incrementor(); • i.makeGUI(); • } • } 实现接口的方法(事件的处理方法) 写main方法进行测试

  9. (作业中)容易犯的错误 • 1、作为类成员变量定义的组件,在构造方法中重新定义,导致执行程序时在事件处理的方法中出现空指针异常NullPointerException • 2、定义的组件数组,没有实例化数组成员,导致执行程序时,引用组件方法时出现空指针异常NullPointerException

  10. 鼠标事件处理 • 鼠标事件的监听器有鼠标事件监听器(MouseListener)、鼠标移动事件监听器(MouseMotionListener)和鼠标滚轮事件监听器(MouseWheelListener) • 鼠标事件对应的类是MouseEvent,其中定义了以下一些常量和方法: MOUSE_PRESSED 鼠标按下 MOUSE_CLICKED 鼠标单击 MOUSE_RELEASED鼠标松开 MOUSE_ENTERED 鼠标进入有鼠标事件监听的容器 MOUSE_EXITED 鼠标离开有鼠标事件监听的容器 getX( ) 取得鼠标的X坐标 getY( ) 取得鼠标的Y坐标 getClickCount( ) 取得鼠标连续单击的次数

  11. 例13:显示鼠标当前的位置 • import java.awt.event.MouseEvent; • import java.awt.event.MouseListener; • import java.awt.event.MouseMotionListener; • import java.awt.Graphics; • import java.awt.BorderLayout; • import java.awt.Container; • import javax.swing.JPanel; • import javax.swing.JFrame; • class MousePanel extends JPanel{ • int x_pos,y_pos; • MousePanel(){ • addMouseListener(new MouseListener(){ • public void mouseClicked(MouseEvent e){} • public void mouseEntered(MouseEvent e){} • public void mouseExited(MouseEvent e){} • public void mousePressed(MouseEvent e){ • x_pos=e.getX(); • y_pos=e.getY(); • repaint(); • } • public void mouseReleased(MouseEvent e){} • });

  12. addMouseMotionListener(new MouseMotionListener(){ • public void mouseMoved(MouseEvent e){ • x_pos=e.getX(); • y_pos=e.getY(); • repaint(); • } • public void mouseDragged(MouseEvent e){} • }); • } • protected void paintComponent(Graphics g){ • super.paintComponent(g); • g.drawString( "当前位置:[" + x_pos + ", " + y_pos + "]", • x_pos, y_pos ); • } • } • public class MouseDemo extends JFrame{ • public MouseDemo( ){ • super( "鼠标位置" ); • Container c = getContentPane( ); • c.add( new MousePanel( ), BorderLayout.CENTER); • } • public static void main(String args[ ]){ • MouseDemo app = new MouseDemo( ); • app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); • app.setSize( 270, 150 ); • app.setVisible( true ); • } • }

  13. 事件适配器类 • Java中为那些具有多个方法的监听者接口提供了事件适配器类,这个类通常命名为XxxAdapter,在该类中以空方法体实现了相应接口的所有方法; • 可通过继承适配器类来编写监听者类,在类中只需给出关心的方法,从而减轻工作量。

  14. 事件接口与适配器类

  15. 事件接口与方法目录

  16. 例13代码修改 • …… • addMouseListener(new MouseAdapter(){ • public void mousePressed(MouseEvent e){ • x_pos=e.getX(); • y_pos=e.getY(); • repaint(); • } • public void mouseReleased(MouseEvent e){} • }); • addMouseMotionListener(new MouseMotionAdapter(){ • public void mouseMoved(MouseEvent e){ • x_pos=e.getX(); • y_pos=e.getY(); • repaint(); • } • }); • ……

  17. 键盘事件 • 键盘事件的监听者对应的接口为KeyListener,适配器为KeyAdapter • 键盘事件对应的类是KeyEvent,其中定义了以下一些常量和方法: • KEY_PRESSED : “按下键”事件。 • KEY_RELEASED :“释放键”事件。 • KEY_TYPED :“键入键”事件。 • VK_* :代表键盘功能键 • char getKeyChar()  返回与此事件中的键相关联的字符。 • int getKeyCode()  返回与此事件中的键相关联的整数keyCode。  • Int getKeyLocation() 返回产生此按键事件的键位置 • static StringgetKeyModifiersText(int modifiers) 返回描述组合键的 String,如“Shift”或“Ctrl+Shift”。 • static StringgetKeyText(int keyCode) 返回描述 keyCode 的 String,如“HOME”、“F1”或“A”。

  18. 例14:键盘事件的例子 • import java.awt.BorderLayout; • import java.awt.Container; • import java.awt.event.FocusEvent; • import java.awt.event.FocusListener; • import java.awt.event.KeyAdapter; • import java.awt.event.KeyEvent; • import javax.swing.JFrame; • import javax.swing.JTextField; • public class KeyboardDemo extends JFrame{ • public KeyboardDemo( ){ • super( "键盘事件处理例程" ); • Container c = getContentPane( ); • JTextField tf = new JTextField("", 15); • tf.addFocusListener( new FocusListener( ){ • public void focusGained(FocusEvent e){ • System.out.println("获得焦点"); • } • public void focusLost(FocusEvent e){ • System.out.println("失去焦点"); • } • } • );

  19. tf.addKeyListener( new KeyAdapter( ){ • public void keyTyped(KeyEvent e){ • System.out.println("键盘事件: " + e.getKeyChar( )); • } • } • ); • c.add( tf, BorderLayout.CENTER ); • } • public static void main(String args[ ]){ • KeyboardDemo app = new KeyboardDemo( ); • app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); • app.setSize( 350, 80 ); • app.setVisible( true ); • } • }

  20. 作业 1、P327 6,7

  21. 作业 2、基于Swing编写一个用户登录的界面程序,用户类型分为学生用户和教师用户,如下图所示。如果用户输入为空,则给出“用户名不可为空”的提示信息,若是教师用户,输入的用户名和密码都是teacher,则显示“教师用户登录成功”的提示信息;若是学生用户,输入的用户名和密码都是student,则显示“学生用户登录成功”的提示信息;否则显示“用户名不存在或者密码不正确”。

  22. 作业 3、使用Swing编写一个支持中文文本编辑程序TextEdit.java,要求如下: • 用户界面大小为400×200像素,如下图所示: • 程序启动后,多行文本输入框JTextArea中显示当前目录下myText.txt文件中原有的内容,如果该文件不存在,则新建该文件。 • “保存”按钮功能:将多行文本输入框JTextArea中的内容写入myText.txt文件中保存。 • “取消”按钮功能:将多行文本输入框TextArea中的内容清空。 • “退出”按钮功能:退出程序

More Related