当前位置: 技术问答>java相关
请教窗口问题!
来源: 互联网 发布时间:2015-09-02
本文导语: 源代码如下: W5.java import java.awt.*; import java.awt.event.*; public class W5 extends Frame implements ActionListener { Button btn; W5() { super("Window5"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEven...
源代码如下:
W5.java
import java.awt.*;
import java.awt.event.*;
public class W5 extends Frame implements ActionListener {
Button btn;
W5() {
super("Window5");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(350,200);
btn=new Button("打开新窗口");
setLayout(new FlowLayout());
add(btn);
btn.addActionListener(this);
setLocation(200,100);
show();
}
public static void main(String args[]) {
new W5();
}
public void actionPerformed(ActionEvent e) {
new W1();
// setLocation(getX()+20,getY()+20);
// toFront();
}
}
W1.java
import java.awt.*;
import java.awt.event.*;
class W1 extends Frame implements WindowListener {
W1() {
super("Window1");
setSize(350,200);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public static void main (String args[]){
new W1();
}
}
用W5打开W1后,将W1关闭时,W5也随之关闭!即System.exit(0);请问有什么方法可以
将W1关闭时,W5不被关闭!多谢!
W5.java
import java.awt.*;
import java.awt.event.*;
public class W5 extends Frame implements ActionListener {
Button btn;
W5() {
super("Window5");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(350,200);
btn=new Button("打开新窗口");
setLayout(new FlowLayout());
add(btn);
btn.addActionListener(this);
setLocation(200,100);
show();
}
public static void main(String args[]) {
new W5();
}
public void actionPerformed(ActionEvent e) {
new W1();
// setLocation(getX()+20,getY()+20);
// toFront();
}
}
W1.java
import java.awt.*;
import java.awt.event.*;
class W1 extends Frame implements WindowListener {
W1() {
super("Window1");
setSize(350,200);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public static void main (String args[]){
new W1();
}
}
用W5打开W1后,将W1关闭时,W5也随之关闭!即System.exit(0);请问有什么方法可以
将W1关闭时,W5不被关闭!多谢!
|
把W1的windowClosing处理改一下:
public void windowClosing(WindowEvent e) {
dispose();
}
public void windowClosing(WindowEvent e) {
dispose();
}