//添加按钮活动监听
import java.awt.*;
import java.awt.event.*;
class AwtDemo2
{
private Frame f;
private Button b;
AwtDemo2()
{
init();
}
public void init()
{
f=new Frame("魔兽世界");
b=new Button("退出游戏");
f.setSize(300,500);
f.setLocation(500,600);
f.setLayout(new FlowLayout());
f.add(b);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("窗体退出");
System.exit(0);
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("按按钮退出");
System.exit(0);
}
});
}
public static void main(String[] args)
{
new AwtDemo2();
}
}
3.加入按钮活动监听
最新推荐文章于 2024-07-08 13:45:00 发布