设为首页 加入收藏

TOP

Java添加事件的四种方式(一)
2014-11-23 20:11:05 来源: 作者: 【 】 浏览:50
Tags:Java 添加 事件 方式

Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动)


/**
* 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() {

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用Java开发一个本地服务管理软件 下一篇SSH+Ajax实现用户名重复检查

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: