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设置为关闭窗口的按钮 展开
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设置为关闭窗口的按钮 展开
2个回答
展开全部
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();
}
}
/**
* 修改后的代码
* 能关闭,
* 希望能帮助你
*/
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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
{
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |