java设置关闭窗口按钮

packagenoname;importjava.awt.*;importjava.awt.event.*;publicclassmainprogramextendsFr... package noname;
import java.awt.*;
import java.awt.event.*;
public class mainprogram extends Frame implements ActionListener,WindowListener
{
int i;
Button b1 = new Button("确定");
Button b2 = new Button("关闭");
void FlowLayoutEX()
{
this.setTitle("布局管理器");
this.setLayout(new FlowLayout());
this.add(b1);
b1.addActionListener(this);
this.add(b2);
b2.addActionListener(this);
this.addWindowListener(this);
this.setBounds(100, 100, 250, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
i++;
Button bi = new Button("按钮"+i);
this.add(bi);
this.show(true);
}
public void windowClosing(WindowEvent f)
{
System.exit(0);
}
public void windowOpened(WindowEvent f){}
public void windowClosed(WindowEvent f){}
public void windowIconified(WindowEvent f){}
public void windowDeiconified(WindowEvent f){}
public void windowActivated(WindowEvent f){}
public void windowDeactivated(WindowEvent f){}
public static void main(String args[])
{
mainprogram window = new mainprogram();
window.FlowLayoutEX();
}
}
这里面的b1已经设置了监听,如何把b2设置为关闭窗口的按钮
展开
 我来答
aqi314819950
2011-02-19 · TA获得超过315个赞
知道小有建树答主
回答量:109
采纳率:0%
帮助的人:131万
展开全部
package org.t20110219.test;

/**
* 修改后的代码
* 能关闭,
* 希望能帮助你
*/
import java.awt.*;
import java.awt.event.*;
public class MainProgram extends Frame implements ActionListener,WindowListener
{
int i;
Button b1 = new Button("确定");
Button b2 = new Button("关闭");
void FlowLayoutEX()
{
this.setTitle("布局管理器");
this.setLayout(new FlowLayout());
this.add(b1);
b1.addActionListener(this);
this.add(b2);
b2.addActionListener(this);
this.addWindowListener(this);
this.setBounds(100, 100, 250, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
/**------------修改代码--------------*/
/**
* 修改 合理就可以了
* e.getSource() 可以得到 你点击了哪个 按钮
* 判断之后就可以 设定 对应的 操作了
*/
if(e.getSource()==b2){
System.exit(0);
}else{
i++;
Button bi = new Button("按钮"+i);
this.add(bi);
this.show(true);
}
}
public void windowClosing(WindowEvent f)
{
System.exit(0);
}
public void windowOpened(WindowEvent f){}
public void windowClosed(WindowEvent f){}
public void windowIconified(WindowEvent f){}
public void windowDeiconified(WindowEvent f){}
public void windowActivated(WindowEvent f){}
public void windowDeactivated(WindowEvent f){}
public static void main(String args[])
{
MainProgram window = new MainProgram();
window.FlowLayoutEX();
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
81684187
2011-02-19 · TA获得超过119个赞
知道小有建树答主
回答量:81
采纳率:0%
帮助的人:86万
展开全部
void FlowLayoutEX()
{
this.setTitle("布局管理器");
this.setLayout(new FlowLayout());
this.add(b1);
b1.addActionListener(this);
this.add(b2);
b2.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
windowClosing(evt);
}
});
this.addWindowListener(this);
this.setBounds(100, 100, 250, 250);
this.setVisible(true);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式