Swing常用事件处理
窗体事件(windowEvent)
鼠标事件(MouseEvent)
键盘事件(KeyEvent)
动作事件(ActionEvent)
窗体事件,指的是对窗体进行操作,例如窗体的打开、关闭、激活、停用等,这些动作都属于窗体事件
java中提供了一个WindowEvent类表示窗体事件。
步骤:
1、手电定义一个实现了WindowListener接口的类作为窗体监听器
2、然后通过addWindowListener()方法将窗体对象与窗体监听器绑定
窗体监听
样例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
public class javagui { public static void main(String[] args) { SwingUtilities.invokeLater(() -> method()); }
private static void method() { JFrame jf = new JFrame("窗体"); jf.setBounds(100,200,300,400);
jf.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { System.out.println("窗体被打开时事件,窗体第一次被打开时触发,只触发一次"); }
@Override public void windowClosing(WindowEvent e) { System.out.println("窗体正在关闭时事件,点击关闭的时候触发"); }
@Override public void windowClosed(WindowEvent e) { System.out.println("窗体关闭事件,无法直观看出,但是关闭之后会执行此处代码"); }
@Override public void windowIconified(WindowEvent e) { System.out.println("窗体的图标化事件,最小化的时候触发"); }
@Override public void windowDeiconified(WindowEvent e) { System.out.println("窗体取消图标化事件,打开最小化的时候触发"); }
@Override public void windowActivated(WindowEvent e) { System.out.println("窗体激活事件,当窗体被调整到所有窗体之前的时候触发,获得焦点"); }
@Override public void windowDeactivated(WindowEvent e) { System.out.println("窗体停用事件,放在其他窗口后面,失去焦点"); } });
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }
}
|
执行效果:
鼠标监听
样例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class javagui { public static void main(String[] args) { SwingUtilities.invokeLater(() -> method()); }
private static void method() { JFrame jf = new JFrame("鼠标监听"); jf.setBounds(100,200,300,400);
JButton btn = new JButton("按钮"); btn.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { System.out.println("鼠标完成单击事件"); }
@Override public void mousePressed(MouseEvent e) { System.out.println("鼠标按下事件"); }
@Override public void mouseReleased(MouseEvent e) { System.out.println("鼠标松开事件"); }
@Override public void mouseEntered(MouseEvent e) { System.out.println("鼠标进入按钮区域事件"); }
@Override public void mouseExited(MouseEvent e) { System.out.println("鼠标移出按钮区域事件"); } });
jf.add(btn);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }
}
|
输出效果:
鼠标事件
MouseEvent.BUTTON1:用来判定鼠标左击事件;
MouseEvent.BUTTON3:用来判定鼠标右击事件;
MouseEvent.BUTTON2:用来判定鼠标滚轮点击事件;
效果演示代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class javagui { public static void main(String[] args) { SwingUtilities.invokeLater(() -> method()); }
private static void method() { JFrame jf = new JFrame("鼠标监听"); jf.setBounds(100,200,300,400);
JButton btn = new JButton("按钮"); btn.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { }
@Override public void mousePressed(MouseEvent e) { System.out.println("鼠标按下事件"); int button = e.getButton(); System.out.println(button); if (button == MouseEvent.BUTTON1){ System.out.println("鼠标左键被点击"); }else if(button == MouseEvent.BUTTON2){ System.out.println("鼠标滚轮被点击"); }else if(button == MouseEvent.BUTTON3){ System.out.println("鼠标右键被点击"); } }
@Override public void mouseReleased(MouseEvent e) { }
@Override public void mouseEntered(MouseEvent e) { }
@Override public void mouseExited(MouseEvent e) { } });
jf.add(btn);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }
}
|
效果截图:
键盘监听
文本框键盘监听:
样例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class javagui { public static void main(String[] args) { SwingUtilities.invokeLater(() -> method()); }
private static void method() { JFrame jf = new JFrame("键盘监听"); jf.setBounds(100,200,300,400);
jf.setLayout(new FlowLayout()); JTextField text = new JTextField(20);
text.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) {
}
@Override public void keyPressed(KeyEvent e) { char c = e.getKeyChar(); System.out.println(c);
int code = e.getKeyCode(); System.out.println("键盘按下的字符内容为:"+c); System.out.println("键盘按下的字符代码为:"+code); }
@Override public void keyReleased(KeyEvent e) {
} });
jf.add(text);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }
}
|
效果截图:
按钮键盘监听
样例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class javagui { public static void main(String[] args) { SwingUtilities.invokeLater(() -> method()); }
private static void method() { JFrame jf = new JFrame("键盘监听"); jf.setBounds(100,200,300,400);
jf.setLayout(new FlowLayout()); JButton btn = new JButton("按钮");
btn.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode();
if (keyCode == 10){ System.out.println("回车被按下"); } } });
jf.add(btn);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }
}
|
效果截图:
动作监听
动作事件用ActionListerter在处理监听的时候会更加方便,不需要重写各种方法了
样例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class javagui { public static void main(String[] args) { SwingUtilities.invokeLater(() -> method()); }
private static void method() { JFrame jf = new JFrame("键盘监听"); jf.setBounds(100,200,300,400);
JButton btn = new JButton("按钮");
btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("利用ActionListernter,进行监听更加方便,不需要重写各种方法"); } });
jf.add(btn);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }
}
|
效果截图:
可以看到和鼠标监听达到的效果是一样的,所有我们通常写程序的时候用的更多的是这种方法。
适配器设计模式:
适用场景:当发现需要使用接口肿的部分功能的时候,可以考虑设计一个适配器(中间类),使用适配器,可以不用重写接口中所有的抽象方法了。