Java监听器
来源:互联网 发布:单片机pwm调速原理 编辑:程序博客网 时间:2024/06/16 14:40
/**
* Java事件监听处理——自身类实现ActionListener接口,作为事件监听器
*
* @author codebrother
*/
class EventListener1 extends JFrame implements ActionListener { private JButton btBlue, btDialog; public EventListener1() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 将按钮添加事件监听器 btBlue.addActionListener(this); btDialog.addActionListener(this); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // ***************************事件处理*************************** @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btBlue) { Container c = getContentPane(); c.setBackground(Color.BLUE); } else if (e.getSource() == btDialog) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }}
/**
* Java事件监听处理——内部类处理
*
* @author codebrother
*/
class EventListener3 extends JFrame { private JButton btBlue, btDialog; // 构造方法 public EventListener3() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 添加事件监听器对象(面向对象思想) btBlue.addActionListener(new ColorEventListener()); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 内部类ColorEventListener,实现ActionListener接口 class ColorEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } } // 内部类DialogEventListener,实现ActionListener接口 class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }}
/**
* Java事件监听处理——匿名内部类处理
*
* @author codebrother
*/
class EventListener2 extends JFrame { private JButton btBlue, btDialog; public EventListener2() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 添加事件监听器(此处即为匿名类) btBlue.addActionListener(new ActionListener() { // 事件处理 @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } }); // 并添加事件监听器 btDialog.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
/**
* Java事件监听处理——外部类处理
*
* @author codebrother
*/
class EventListener4 extends JFrame { private JButton btBlue, btDialog; public EventListener4() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 将按钮添加事件监听器 btBlue.addActionListener(new ColorEventListener(this)); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}// 外部类ColorEventListener,实现ActionListener接口class ColorEventListener implements ActionListener { private EventListener4 el; ColorEventListener(EventListener4 el) { this.el = el; } @Override public void actionPerformed(ActionEvent e) { Container c = el.getContentPane(); c.setBackground(Color.BLUE); }}// 外部类DialogEventListener,实现ActionListener接口class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); }}public class ActionListenerTest{ public static void main(String args[]) { new EventListener2(); }}
阅读全文
0 0
- java 监听器
- java监听器
- java监听器
- Java 监听器
- JAVA监听器
- java监听器
- Java监听器
- java 监听器
- java监听器
- java 监听器
- java 监听器
- java监听器
- java 监听器
- Java监听器
- java监听器
- Java监听器
- 监听器---java
- Java 监听器
- 关于cliptopadding用法(仅限于个人收藏)
- QT中的QInputDialog的小例子
- 欢迎使用CSDN-markdown编辑器
- spring+hibernate出错小结
- 模仿R语言c++ 向量类c 矩阵类matrix等(持续更新 欢迎指点)
- Java监听器
- docker学习
- 猜数字小游戏(有次数限制)
- C++抽象编程——算法分析(7)——快速排序算法分析
- 事件绑定方法live和bind的区别及使用场合
- Eclipse 安装 SVN 插件的两种方法
- java多线程(五) 之 设计线程安全的类
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) cause instance crash
- 网络编程·十二