求教awt关闭窗口的问题

lucima 2007-11-27 10:44:49
package awt;

import java.awt.*;
import java.awt.event.*;
public class ExGui{
private Frame f;
private Button b1;
private Button b2;
public ExGui(){
// this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
//这段code放在下面的函数里也不能起到,一点窗口的叉子就关闭窗口的作用。
}
private void addWindowListener(WindowAdapter windowAdapter) {
// TODO Auto-generated method stub

}
public static void main(String args[]){
ExGui that = new ExGui();

that.go();
}

public void go(){
f = new Frame("GUI example");
f.setLayout(new FlowLayout());
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮

f.setVisible(true);
}


}
...全文
710 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
崔小涣 2012-10-04
  • 打赏
  • 举报
回复
初学者同样遇到窗口关闭不了的问题,已找到解决办法。
cursor_wang 2007-11-28
  • 打赏
  • 举报
回复
对了,你要先把Frame f= new Frame("GUI example");构造好,不然会空指针异常.你放在go()里面构造可能不行的,因为先用构造函数.
cursor_wang 2007-11-28
  • 打赏
  • 举报
回复
public ExGui(){
// this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
//这段code放在下面的函数里也不能起到,一点窗口的叉子就关闭窗口的作用。
}
-------------------------------------------------------------------
你这段代码没有错,我看了,你把this换成f就可以了.
Dan1980 2007-11-28
  • 打赏
  • 举报
回复
楼主的程序简直是一团乱麻啊,你只要显示一个Frame就可以了,怎么new了那么多Frame出来?

还有,你没有搞清楚你是用继承方式还是组合方式。

继承方式的话,就让你的类extends Frame,然后直接在构造方法中addWindowListener(new WindowAdapter() { ... })就可以了。

组合方式的话,不需要extends Frame,但要设置一个Frame类型的成员变量,并在构造函数中初始化它,再对该成员调用addWindowListener(new WindowAdapter() { ... })。

下面分别是我整理的继承方式和组合方式的代码,供你参考:

// 继承方式使用Frame
import java.awt.*;
import java.awt.event.*;

public class ExGui extends Frame {
private Button b1;
private Button b2;

public ExGui() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String args[]) {
ExGui that = new ExGui();
that.go();
}

public void go() {
setLayout(new FlowLayout(FlowLayout.RIGHT));
b1 = new Button("Press Me");
b2 = new Button("Don't Press Me");
add(b1);
add(b2);
setSize(555, 666);
setVisible(true);
}

}


// 组合方式使用Frame
import java.awt.*;
import java.awt.event.*;

public class ExGui {
private Frame f;
private Button b1;
private Button b2;

public ExGui() {
f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String args[]) {
ExGui that = new ExGui();
that.go();
}

public void go() {
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
b1 = new Button("Press Me");
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.setSize(555, 666);
f.setVisible(true);
}

}


另外,setSize()和pack()用其中一个就可以了。
cursor_wang 2007-11-28
  • 打赏
  • 举报
回复
我改快了,构造函数里这句Frame f = new Frame();可以不要.要处理事件构造函数里添加b1.addActionListener(this);b2.addActionListener(this);
cursor_wang 2007-11-28
  • 打赏
  • 举报
回复
我叫你初始化在构造函数做,你不听,帮你改好了,事件处理在public void actionPerformed(ActionEvent ae)这里面做.可以了就结贴.
import java.awt.*;
import java.awt.event.*;
public class ExGui extends Frame implements ActionListener{
private Frame f= new Frame("GUI example");
private Button b1;
private Button b2;
public ExGui(){
Frame f = new Frame();
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
f.setSize(555,666);
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮

f.setVisible(true);
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
}

public void actionPerformed(ActionEvent ae)
{

}
public static void main(String args[]){
ExGui that = new ExGui();
}


}
lucima 2007-11-28
  • 打赏
  • 举报
回复
package awt;//还是不行。窗口事件接收不到。

import java.awt.*;
import java.awt.event.*;
public class ExGui{
private Frame f;
private Button b1;
private Button b2;
public ExGui(){
Frame f = new Frame();
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
}
private void addWindowListener(WindowAdapter windowAdapter) {
// TODO Auto-generated method stub

}
public static void main(String args[]){
ExGui that = new ExGui();

that.go();
}

public void go(){
Frame f = new Frame("GUI example");
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
f.setSize(555,666);
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮

f.setVisible(true);
}


}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧