Java 如何显示和隐藏现有JFrame
我正在做一些基本的测试来了解JavaSwing是如何工作的 我有一个测试应用程序,它由三个完全独立的windows JFrames组成: 主菜单 资产窗口1 资产窗口2 主菜单有一个JButton,它将显示/隐藏资产窗口1 a1 这是启动所有windows的主类: 包测试1; 导入test1.AssetList.AssetList; 导入test1.MainMenu.MainMenu; 导入javax.swing.*; 公共类Test1{ 公共静态无效字符串[]args{ javax.swing.SwingUtilities.invokelaterNewRunnable{ 公开募捐{ 主菜单m=新主菜单; 资产列表a1=新资产列表; 资产清单a2=新资产清单; } }; } } 这是具有资产窗口JFrame的类: 包test1.AssetList; 导入javax.swing.*; 公共类AssetList扩展了JFrame{ 公共资产清单{ JLabel标签=新的JLabelAsset列表; this.getContentPane.addlabel; 这个包; 此.setVisiblefalse; } } 这是主菜单JFrame的类: 包test1.main菜单; 导入javax.swing.JFrame; 导入javax.swing.JLabel; 导入javax.swing.JButton; 公共类主菜单扩展JFrame{ JLabel标签=新的JLabelMain菜单; JButton按钮=新JButtonAsset; 公共主菜单{ this.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE; this.getContentPane.addlabel; this.getContentPane.addbutton; button.AddActionListener新建ButtonAssetListener; 这个包; 此.setVisibletrue; } } 这是资产窗口按钮JButton侦听器的类: 包test1.main菜单; 导入java.awt.event.ActionEvent; 导入java.awt.event.ActionListener; 公共类ButtonAssetListener实现ActionListener{ @凌驾 公共无效行动执行行动事件evt{ System.out.printlnCLICK!; /*伪码 来自Test1.isVisible的ifa1==true{ a1来自Test1.setVisiblefalse; }否则{ a1来自Test1.setVisibletrue; } */ } } 如何从ButtonAsetListener检索a1实例以切换其可见性?Java 如何显示和隐藏现有JFrame,java,swing,Java,Swing,我正在做一些基本的测试来了解JavaSwing是如何工作的 我有一个测试应用程序,它由三个完全独立的windows JFrames组成: 主菜单 资产窗口1 资产窗口2 主菜单有一个JButton,它将显示/隐藏资产窗口1 a1 这是启动所有windows的主类: 包测试1; 导入test1.AssetList.AssetList; 导入test1.MainMenu.MainMenu; 导入javax.swing.*; 公共类Test1{ 公共静态无效字符串[]args{ javax.swing
在Java Swing中构造这种多窗口应用程序有更好的替代方法吗?您只需将要隐藏的实例传递给按钮侦听器即可
public class Test1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
AssetList a1 = new AssetList();
AssetList a2 = new AssetList();
MainMenu m = new MainMenu(a1);
}
});
}
}
让主菜单包含一个它将显示和隐藏的组件
public class MainMenu extends JFrame {
JLabel label = new JLabel("Main Menu");
JButton button = new JButton("Asset");
public MainMenu(JComponent assetList) {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(label);
this.getContentPane().add(button);
button.addActionListener(new ButtonAssetListener(assetList));
this.pack();
this.setVisible(true);
}
}
然后修改按钮资源侦听器,以接收一个组件,然后显示或隐藏该组件
public class ButtonAssetListener implements ActionListener{
private JComponent component;
public ButtonAssetListener(JComponent component) {
this.component = component;
}
@Override
public void actionPerformed(ActionEvent evt) {
if(component.isVisible()) {
component.setVisible(false);
} else {
component.setVisible(true);
}
}
}
您不应该使用多个帧。如果您需要多个窗口,请在主父框架的子窗口中使用JDialog。请参阅@andrewhompson谢谢您的评论。我将通读这篇文章即使在一个主父框架中使用多个JDialogs作为子窗口,访问其他窗口的问题仍然存在。@andherthompson在阅读这篇文章之后,我还不是100%清楚我是否应该使用多个JFrames。我的应用程序将是多显示器和完全可自定义的布局必须允许。每个窗口都是自主的,并且与其他窗口松散相关。我可能会对这两种解决方案进行测试。有人建议我在堆栈溢出之外使用带有JFrames的类作为静态资源。类可以跨不同的实体实例化,并且可以访问JFrames。有人建议我在单用户应用程序中使用此解决方案。在处理大量窗口时,跨不同类移动参数可能会很麻烦。@M.E.我大体上同意这一点。但是,在您的情况下,您有一个或多个AssetList框架,而在许多应用程序中,您有一个WinMain类型类。这个解决方案只是其中一种方法,也许你想用一个注册windows的注册表做点什么,然后你的主菜单可以查看这个注册表,看看那里有什么。对于您的用例来说,这可能是一种更好的方式。感谢您对您的答案作进一步的评论。作为建议的一部分,提出了您提到的注册表方法。到目前为止,我只是在做一些非常基本的步骤,因为我需要学习Swing。过去我在其他语言中使用过其他小部件框架,但不是Swing,但如果这有意义的话,我将进入某种类型的注册表,这可能是因为窗口的数量是可变的。我将逐步将所有这些纳入其中。