Java图形界面——事件监听与处理
2017-05-23 08:27
731 查看
/*
* 事件监听与处理
*/
package com.test.tank;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Test2 extends JFrame implements ActionListener{
JPanel jp = null;
JButton jbt1 = null;
JButton jbt2 = null;
public static void main(String[] args) {
Test2 test = new Test2();
}
public Test2(){
jp = new JPanel();
jbt1 = new JButton("红色");
jbt1.addActionListener(this); //注册监听 ; this代表Test2这个类(对象)
jbt1.setActionCommand("red"); //设置命令
jbt2 = new JButton("蓝色");
jbt2.addActionListener(this); //注册监听
jbt2.setActionCommand("blue"); //设置命令
Other other = new Other();
jbt2.addActionListener(other); //让Other类的对象也注册监听jbt2
//添加组件并布局
this.add(jbt1, BorderLayout.WEST);
this.add(jp, BorderLayout.CENTER);
jp.setBackground(Color.black);
this.add(jbt2, BorderLayout.EAST);
this.setSize(300, 200);
this.setTitle("事件监听与处理");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override //对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("red")){//命令相同,则执行对应操作
jp.setBackground(Color.red);
}
if(e.getActionCommand().equals("blue")){
jp.setBackground(Color.blue);
}
}
}
/*
* 测试:同一个事件可让多个监听者(对象)
*/
class Other implements ActionListener{
@Override //也必须重写对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().endsWith("blue")){
System.out.println("其他对象也监听必处理了jbt2事件源");
}
}
}
* 事件监听与处理
*/
package com.test.tank;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Test2 extends JFrame implements ActionListener{
JPanel jp = null;
JButton jbt1 = null;
JButton jbt2 = null;
public static void main(String[] args) {
Test2 test = new Test2();
}
public Test2(){
jp = new JPanel();
jbt1 = new JButton("红色");
jbt1.addActionListener(this); //注册监听 ; this代表Test2这个类(对象)
jbt1.setActionCommand("red"); //设置命令
jbt2 = new JButton("蓝色");
jbt2.addActionListener(this); //注册监听
jbt2.setActionCommand("blue"); //设置命令
Other other = new Other();
jbt2.addActionListener(other); //让Other类的对象也注册监听jbt2
//添加组件并布局
this.add(jbt1, BorderLayout.WEST);
this.add(jp, BorderLayout.CENTER);
jp.setBackground(Color.black);
this.add(jbt2, BorderLayout.EAST);
this.setSize(300, 200);
this.setTitle("事件监听与处理");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override //对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("red")){//命令相同,则执行对应操作
jp.setBackground(Color.red);
}
if(e.getActionCommand().equals("blue")){
jp.setBackground(Color.blue);
}
}
}
/*
* 测试:同一个事件可让多个监听者(对象)
*/
class Other implements ActionListener{
@Override //也必须重写对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().endsWith("blue")){
System.out.println("其他对象也监听必处理了jbt2事件源");
}
}
}
相关文章推荐
- Java图形界面——事件监听与处理
- Java图形界面事件监听处理之四种方法
- Java图形界面事件监听处理之四种方法
- Java图形界面事件监听处理之四种方法
- **JAVA** GUI图形界面编程-事件监听机制
- Java GUI之事件监听与处理的匿名类实现方法
- JAVA事件处理,和事件监听机制
- Java图形界面中单选按钮JRadioButton和按钮Button事件处理
- Java GUI之事件监听与处理
- Java — 事件监听、事件处理 初体验
- Java Swing事件处理——键盘事件及监听处理 KeyListener 按键测试
- java事件监听机制中的事件、监听器接口、处理方法、事件源
- 18. JAVA 图形界面 Part 2(JTextComponent、事件处理、单选按钮、复选框、列表框、下拉列表框、菜单组件、文件选择) ----- 学习笔记
- Java Swing界面编程(24)---事件处理:键盘事件及监听处理
- Java笔记(10)-图形界面设计、Swing、窗口、JFrame、常用组件和布局、处理事件、MVC结构、对话框、GUI
- 初涉java(事件处理与事件监听)
- Java 事件和监听器 监听处理小框架
- Java GUI之事件监听与处理
- Java 图形界面事件处理机制
- Java图形界面:事件监听