按钮点击产生的效果通过事件监听来实现,下面介绍如何创建一个按钮的监听器,
一、创建监听器
1. 创建一个普通的Frame和然后添加一个按钮,参考教程
2.自制一个MyActionListener 的监听类
2.1 MyActionListener 需要实现implements接口ActionListener
public class MyActionListener implements ActionListener
2.2 重写唯一的actionPersformed(ActionEvent e)方法,
2.3 e.getActionCommand() 返回的是e 的触发事件的动作命令 (idea中ctrl+左键)来查看源码
代码如下
package GUI.事件监听;
import GUI.MyClass.MySystemExit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test2两个按钮实现同一个监听 {
public static void main(String[] args) {
Frame frame = new Frame("Test2两个按钮实现同一个监听");
frame.setVisible(true);
Button north = new Button("north");
Button south = new Button("south");
//自定义触发会显示的ActionCommand 默认 为Button("...");中的值.
//add listener for the south and north...
north.addActionListener(new MyMonitor());
south.addActionListener(new MyMonitor());
//2个按钮add the same ActionListener
frame.add(north, BorderLayout.NORTH);
frame.add(south, BorderLayout.SOUTH);
//l,s,c
frame.setLocation(100, 100);
frame.setSize(400, 400);
frame.setBackground(new Color(99, 255, 240));
//System.exit(0);
new MySystemExit(frame);
}
private static class MyMonitor implements ActionListener {
//build the ActionLister for the north button and the south button ,named myActionListener
@Override
public void actionPerformed(ActionEvent e) {
//输入 e. 查看源码.
if (e.getActionCommand() == "north") {
System.out.println("north Button been clicked ,and MyMonitor class run successfully.");
} else if (e.getActionCommand() == "south") {
System.out.println("south Button been clicked ,and MyMonitor class run successfully.");
}
}
}
}
3.新建事件对象,将其添加到按钮中.
MyActionListener myActionListener = new MyActionListener();
north.addActionListener(myActionListener);
south.addActionListener(myActionListener);
总代码, Demo.java
package GUI.事件监听;
import GUI.MyClass.MyActionListener;
import GUI.MyClass.MySystemExit;
import java.awt.*;
/**
* 自制一个事件监听类,添加到按钮里。
*/
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame("事件监听");
Button north = new Button("north");
Button south = new Button("south");
//因为addActionListener(...) 需要一个 ActionListener 所以我门自己建造一个 MyActionListener
//查看源码得知,public synchronized void addActionListener(ActionListener l)
MyActionListener myActionListener = new MyActionListener();
north.addActionListener(myActionListener);
south.addActionListener(myActionListener);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.pack();//打包.pack()
//color location size
frame.setLocation(100,100);
frame.setSize(400,400);
frame.setBackground(new Color(99, 255, 240));
//可见性.
frame.setVisible(true);
//add ActionListener.
new MySystemExit(frame);
}
}
执行窗口退出的类 MySystemExit.java
package GUI.MyClass;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MySystemExit {
public MySystemExit(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("System.exit(0)");
System.exit(0);
}
});
}
}