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() {
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) {mvc实例
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();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论