java 怎么用setVisible(false);方法隐藏其他窗口

this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
denlu b=new denlu();
b.setVisible(false);
我写了这条语句希望本窗口被打开时隐藏名为“denlu”的窗口,没有成功,不知为什么。

要隐藏其它窗口你得获取到那个窗口对象,然后调用这个方法,获取窗口对象你可以通过传参的方式把那个窗口对象带过来或者直接传到构造器里,也可以通过获取父级也就是.PARENT()这种思路去获取到你要隐藏的窗口对象,实在不行你就把那货存成静态的吧,这样哪都能隐藏了
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-27
this.setVisible(false);//隐藏当前窗体,如果当前窗体Frame的名为f则写成:f.setVisible(false);
new JFrameTest();//要新出来的窗体

如点击按钮出现一新的窗口,3秒后消失。
import java.awt.*;
import java.awt.event.*;

public class HideWind extends Frame {
Button b = new Button("window");

double heigth, weigth;
double x, y;
TextField tf1 = new TextField(null, 10);
TextField tf2 = new TextField(null, 10);

public HideWind(String name) {
super(name);
setLayout(new FlowLayout(FlowLayout.LEFT));

add(tf1);

add(tf2);
add(b);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

HideWind w = new HideWind("bbbbbbbbbbbb");
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

w.setVisible(false);

}

}

public static void main(String[] args) {
new HideWind("windows1");
}

}本回答被提问者和网友采纳
相似回答