一、动作事件类监听(ActionEvent)
典型事件源:JButton,JRadioButton,JList,JMenuItem
JButton运用内部类实现监听实例:
package GUIProgramming;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestActionListener extends Jframe { private JButton button = new JButton("ok"); private JLabel label = new JLabel(); public TestActionListener(){ //构造方法 this.setBounds(200,200,600,400); this.setBackground(Color.yellow); this.setLayout(new FlowLayout(FlowLayout.CENTER)); this.setVisible(true); this.add(button); this.add(label); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //Jframe的窗口关闭方法 //新建JButton事件监听类对象 ButtonActionListener buttonListener = new ButtonActionListener(); button.addActionListener(buttonListener); } //定义JButton事件监听类进行事件处理(内部类) class ButtonActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //实现动作处理方法 label.setText("666"); //内部类访问外部类对象私有成员 } } public static void main(String[] args) { new TestActionListener(); }}
改为匿名内部类:
package GUIProgramming;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestActionListener extends Jframe { private JButton button = new JButton("ok"); private JLabel label = new JLabel(); public TestActionListener(){ //构造方法 this.setBounds(200,200,600,400); this.setBackground(Color.yellow); this.setLayout(new FlowLayout(FlowLayout.CENTER)); this.setVisible(true); this.add(button); this.add(label); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //Jframe的窗口关闭方法 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("666"); } }); } public static void main(String[] args) { new TestActionListener(); }}
lambda表达式
button.addActionListener((e) -> { label.setText("666"); });
二、选择事件类(ItemEvent)
典型事件源:Checkbox,JList,JComboBox
复选框Checkbox监听:
package GUIProgramming;import javax.swing.*;import java.awt.*;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;public class TestCheckbox extends Jframe { private Checkbox box1 = new Checkbox("study",false);//定义复选框 private Checkbox box2 = new Checkbox("sleep",false); private Checkbox box3 = new Checkbox("eat",false); class itemSelected implements ItemListener{//设置监听者(内部类) @Override public void itemStateChanged(ItemEvent e) { String str = "you want "; if(box1.getState() == true) //选中box1 str = str + "study "; if(box2.getState() == true) str = str + "sleep "; if(box3.getState() == true) str = str + "eat "; new MyDialog(str); //新建一个弹窗,显示字符串 } } public TestCheckbox(){ //构造方法 itemSelected itemSelected = new itemSelected();//添加监听器 box1.addItemListener(itemSelected); box2.addItemListener(itemSelected); box3.addItemListener(itemSelected); this.add(box1);//添加组件 this.add(box2); this.add(box3); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗口 this.setLayout(new FlowLayout(FlowLayout.CENTER)); this.setVisible(true); } public static void main(String[] args) { TestCheckbox testCheckbox = new TestCheckbox();//新建窗口对象 testCheckbox.setBounds(500,500,600,400); testCheckbox.setBackground(Color.CYAN); }}class MyDialog extends JDialog{//定义一个弹窗类显示字符串 public MyDialog(String str){ this.setVisible(true); this.setBounds(400,400,300,200); this.setTitle(str); }}
三、文本事件类(TextEvent)
典型事件源:JTextField,Jtextarea
文本框TextField:
package GUIProgramming;import javax.swing.*;import java.awt.*;import java.awt.event.TextEvent;import java.awt.event.TextListener;public class TestTextField extends Jframe { TextField textField = new TextField(20); //ActionListener 的 actionPerformed按下回车调用 class TextFieldListener implements TextListener{ @Override//每当输入框中内容改变时调用 public void textValueChanged(TextEvent e) { TextField textField = (TextField) e.getSource(); //造型 if(textField.getText().equals("666233")) System.out.println("666"); else System.out.println("233"); } } public TestTextField() { textField.addTextListener(new TextFieldListener()); add(textField); setBounds(400,400,500,300); setBackground(Color.pink); setVisible(true); setLayout(new FlowLayout()); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextField(); }}
四、窗口事件类(WindowEvent)
典型事件源:Jframe
frame窗口的关闭(匿名内部类实现):
package GUIProgramming;import javax.swing.*;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestWindow extends frame { Button button = new Button(); public TestWindow() { add(button); setBounds(400,400,500,300); setBackground(Color.pink); setVisible(true); setLayout(new FlowLayout()); //添加窗口监听事件 addWindowListener(new WindowAdapter() { //匿名内部类(实现接口) @Override public void windowClosing(WindowEvent e) { System.exit(0); //正常退出程序 } }); } public static void main(String[] args) { new TestWindow(); }}
五、键盘事件类(KeyEvent)
典型事件源:TextField,textarea
简单的直接判断按键实例:
package GUIProgramming;import javax.swing.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class TestKeyEvent extends Jframe { //内部类 //class test implements KeyListener(重写接口中的所有方法) class testKey extends KeyAdapter {//extends keAdapter抽象类(选择要使用的方法) @Override public void keyPressed(KeyEvent e) { //按下按键时调用 int key = e.getKeyCode(); //获取按下的键位编码 if(key == KeyEvent.VK_C)//按下 C 键结束程序 System.exit(0); } } public TestKeyEvent() { addKeyListener(new testKey()); //添加键盘监听器 setBounds(500,500,600,400); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestKeyEvent(); }}
六、鼠标事件类(MouseEvent)
典型事件源:Panel,Canvas(画布)
package GUIProgramming;import javax.swing.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class TestMouseEvent extends Jframe { class testMouseEvent extends MouseAdapter{ //适配器(内部类) @Override public void mousePressed(MouseEvent e) {//鼠标被按下时调用 System.out.println("(" + e.getX() + "," + e.getY() + ")"); System.out.println(e.getPoint());//输出当前点的坐标 } } public TestMouseEvent() { //构造方法 addMouseListener(new testMouseEvent()); setBounds(500,500,600,400); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestMouseEvent(); }}