62,620
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent1
{
public static void main(String[] args)
{
Frame f=new Frame("test");
f.setBounds(300,300,200,200);
Button b1=new Button("start");
Button b2=new Button("stop");
Monitor bh=new Monitor();//自定义的类
b1.addActionListener(bh);
b2.addActionListener(bh);
b2.setActionCommand("game over");
f.add(b1,BorderLayout.CENTER);
f.add(b2,BorderLayout.NORTH);
f.pack();//Frame.pack()这个方法的作用就是根据窗口里面的布局及组件的preferedSize来确定frame的最佳大小。
f.setVisible(true);
}
}
class Monitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("don't press me!!!!!!!!!!!!"+e.getActionCommand());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "新建") {
newFiel();
} else if (e.getActionCommand() == "打开") {
,白洁; openFile();
} else if (e.getActionCommand() == "保留") {
saveFiel();
,白洁;} else if (e.getActionCommand() == "退出") {
System.exit(0);
}
}