打开多窗口程序的时候,怎么在打开另一个的时候隐藏前一个? 请问:打开多窗口程序的时候,怎么在打开另一个的时候隐藏前一个窗口?我在第一个程序里设了一个boolean的变量isopen,想通过改变isopen变量的值来达到隐藏效果,可达不到,请问怎么作? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的测试程序如下:import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;public class MainFrame extends JFrame { /** * @param args */ private JButton b1=new JButton("ok"); private static boolean isopen=true; public MainFrame() { Container cp=this.getContentPane(); cp.setLayout(new BorderLayout()); cp.add(b1,BorderLayout.CENTER); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub ChildFrame ch=new ChildFrame(); isopen=false; } } ); } public static void main(String[] args) { // TODO Auto-generated method stub MainFrame mf=new MainFrame(); mf.setTitle("MainFrame"); mf.setSize(200, 100); mf.setLocationRelativeTo(null); mf.setVisible(isopen); }} 你在JButton的动作监听中已经实现了一个新的窗口,只要再下一行把旧窗口mf.setVisible(false);就可以了。import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;public class MainFrame extends JFrame { private MainFrame mf = null; private JButton b1=new JButton("ok"); private static boolean isopen=true; public MainFrame() { mf = this; Container cp=this.getContentPane(); cp.setLayout(new BorderLayout()); cp.add(b1,BorderLayout.CENTER); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ChildFrame ch=new ChildFrame(); mf.setVisible(false); } }); mf.setTitle("MainFrame"); mf.setSize(200, 100); mf.setLocationRelativeTo(null); mf.setVisible(true); } public static void main(String[] args) { new MainFrame(); }}class ChildFrame extends JFrame { public ChildFrame(){ this.setSize(200, 100); this.setVisible(true); }} 你根本用不着isopen这个变量,你用它还出现了好多错误呢!特别静态变量不会用就不要乱用.楼上说得太对了,你的思路不对.mf.setVisible(isopen)这一句只会执行一次,就算你改变了isopen这个值,也不会隐藏窗口的.重新写一个吧. 关于正则表达式请教! 高分100 求代码 两行数据找相同元素 请教SWT录象正在播放 直接关闭view 导致整个程序卡死的问题 一道简单的JAVA题 急 急!!!! 如何提高计算机的英文水平呢? java可否调用win32API呢 关于socket Thread.currentThread().sleep(5000)跟Thread.sleep(5000)有什么不同呢? 哪里有详细的介绍JDBC API的网站? 请问IDEA的热键怎么更改啊 请教怎么在Java中实现读取保存Flash中数据的问题
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class MainFrame extends JFrame { /**
* @param args
*/
private JButton b1=new JButton("ok");
private static boolean isopen=true;
public MainFrame()
{
Container cp=this.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1,BorderLayout.CENTER);
b1.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ChildFrame ch=new ChildFrame();
isopen=false;
}
}
);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MainFrame mf=new MainFrame();
mf.setTitle("MainFrame");
mf.setSize(200, 100);
mf.setLocationRelativeTo(null);
mf.setVisible(isopen);
}}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class MainFrame extends JFrame {
private MainFrame mf = null;
private JButton b1=new JButton("ok");
private static boolean isopen=true;
public MainFrame() {
mf = this;
Container cp=this.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1,BorderLayout.CENTER);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChildFrame ch=new ChildFrame();
mf.setVisible(false);
}
});
mf.setTitle("MainFrame");
mf.setSize(200, 100);
mf.setLocationRelativeTo(null);
mf.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}}class ChildFrame extends JFrame {
public ChildFrame(){
this.setSize(200, 100);
this.setVisible(true);
}
}
楼上说得太对了,你的思路不对.mf.setVisible(isopen)这一句只会执行一次,就算你改变了isopen这个值,也不会隐藏窗口的.重新写一个吧.