标签:rgs 停止 print start listen dap lang err win
public class Demo01 {
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame = new Frame();
Button button = new Button();
//因为addActionListener需要ActionListener,因此我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
public static void windowClose(Frame frame) {
//关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//事件监听
static class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("触发事件!");
}
}
}
public class Demo02 {
public static void main(String[] args) {
//两个按钮实现同一个监听
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则默认值
button2.setActionCommand("按钮2-stop");
Monitor monitor = new Monitor();
button1.addActionListener(monitor);
button2.addActionListener(monitor);
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
class Monitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获得按钮的信息
System.out.println("点击"+e.getActionCommand());
}
}
标签:rgs 停止 print start listen dap lang err win
原文地址:https://www.cnblogs.com/saxonsong/p/14656604.html