java SWing事件调用的两种机制
2017-08-29 16:19
127 查看
Java(91)
[java] view plain copy
/**
* java swing中事件调用的两种机制:
* (一)响应机制
* (二)回调机制
*/
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SimpleListener implements ActionListener {
/*
* 利用该类来监听事件源产生的事件,利用响应机制
*/
public void actionPerformed(ActionEvent e) {
String buttonName = e.getActionCommand();
if (buttonName.equals("按钮1"))
System.out.println("按钮1 被点击");
}
}
/*
* 利用该类来处理事件源产生的事件,利用回调机制
*/
class ButtonAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
System.out.println("按钮2 被点击");
}
}
public class ActionTest {
private static JFrame frame; // 定义为静态变量以便main使用
private static JPanel myPanel; // 该面板用来放置按钮组件
private JButton button1; // 这里定义按钮组件
private JButton button2;
public ActionTest() { // 构造器, 建立图形界面
// 新建面板
myPanel = new JPanel();
// 新建按钮
button1 = new JButton("按钮1"); // 新建按钮1
// 建立一个actionlistener让按钮1注册,以便响应事件
SimpleListener ourListener = new SimpleListener();
button1.addActionListener(ourListener);
button2 = new JButton();// 新建按钮2
// 建立一个ButtonAction注入按钮2,以便响应事件
ButtonAction action = new ButtonAction();
button2.setAction(action);
button2.setText("按钮2");
myPanel.add(button1); // 添加按钮到面板
myPanel.add(button2);
}
public static void main(String s[]) {
ActionTest gui = new ActionTest(); // 新建Simple1组件
frame = new JFrame("Simple1"); // 新建JFrame
// 处理关闭事件的通常方法
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(myPanel);
frame.pack();
frame.setVisible(true);
}
}
顶1
[java] view plain copy
/**
* java swing中事件调用的两种机制:
* (一)响应机制
* (二)回调机制
*/
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SimpleListener implements ActionListener {
/*
* 利用该类来监听事件源产生的事件,利用响应机制
*/
public void actionPerformed(ActionEvent e) {
String buttonName = e.getActionCommand();
if (buttonName.equals("按钮1"))
System.out.println("按钮1 被点击");
}
}
/*
* 利用该类来处理事件源产生的事件,利用回调机制
*/
class ButtonAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
System.out.println("按钮2 被点击");
}
}
public class ActionTest {
private static JFrame frame; // 定义为静态变量以便main使用
private static JPanel myPanel; // 该面板用来放置按钮组件
private JButton button1; // 这里定义按钮组件
private JButton button2;
public ActionTest() { // 构造器, 建立图形界面
// 新建面板
myPanel = new JPanel();
// 新建按钮
button1 = new JButton("按钮1"); // 新建按钮1
// 建立一个actionlistener让按钮1注册,以便响应事件
SimpleListener ourListener = new SimpleListener();
button1.addActionListener(ourListener);
button2 = new JButton();// 新建按钮2
// 建立一个ButtonAction注入按钮2,以便响应事件
ButtonAction action = new ButtonAction();
button2.setAction(action);
button2.setText("按钮2");
myPanel.add(button1); // 添加按钮到面板
myPanel.add(button2);
}
public static void main(String s[]) {
ActionTest gui = new ActionTest(); // 新建Simple1组件
frame = new JFrame("Simple1"); // 新建JFrame
// 处理关闭事件的通常方法
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(myPanel);
frame.pack();
frame.setVisible(true);
}
}
顶1
相关文章推荐
- java SWing事件调用的两种机制
- Android的两种事件处理机制
- Java Swing中有关事件机制
- 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制
- ViewPager滑动事件调用机制简单概述
- 详解Android的两种事件处理机制
- C#重绘OnPaint()事件调用机制
- Java swing(awt):事件监听机制的实现原理+简单示例
- [置顶] 安卓中滑动事件的传递机制及dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent的调用
- 事件机制 as与mxml 两种方式代码
- 视窗图形系统的两种事件消息实现机制
- 转载NET体系下的回调与异步调用之委托与事件机制
- Andoid Touch事件分发机制 及与OnClick的调用机制
- swt 中的事件调用机制
- Android平台的事件处理机制有两种
- 服务端控件开发 包含处理机制—— 子控件中调用住空间委托的事件实例
- 使用事件机制相比直接调用函数的优势
- 反射机制进行动态调用的两种创建方法实例与hama中的反射机制
- Cocos2d-x之Touch事件处理机制 提供两种触摸事件处理机制:CCStandardTouchDelegate和CCTargetedTouchDelegate。
- JavaScript中external与active host之间的事件调用机制