繁体   English   中英

如何显示和隐藏现有的JFrame

[英]How to show and hide an existing JFrame

我正在做一些基本测试,以了解Java Swing的工作原理。

我有一个包含三个完全独立的窗口(JFrames)的测试应用程序:

  1. 主菜单
  2. 资产窗口1
  3. 资产窗口2

主菜单有一个JButton,它将显示/隐藏资产窗口1a1 )。

这是启动所有Windows的主要类:

package test1;

import test1.AssetList.AssetList;
import test1.MainMenu.MainMenu;
import javax.swing.*;

public class Test1 {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainMenu m = new MainMenu();
                AssetList a1 = new AssetList();
                AssetList a2 = new AssetList();
            }
        });
    }
}

这是资产窗口 JFrame的类:

package test1.AssetList;

import javax.swing.*;

public class AssetList extends JFrame {

    public AssetList() {

        JLabel label = new JLabel("Asset list");
        this.getContentPane().add(label);

        this.pack();
        this.setVisible(false);

    }

}

这是MainMenu JFrame的类:

package test1.MainMenu;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;

public class MainMenu extends JFrame {

    JLabel label = new JLabel("Main Menu");
    JButton button = new JButton("Asset");  

    public MainMenu() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.getContentPane().add(label);
        this.getContentPane().add(button);

        button.addActionListener(new ButtonAssetListener());

        this.pack();
        this.setVisible(true);

    }

}

这是“ 资产窗口按钮” JButton侦听器的类:

package test1.MainMenu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonAssetListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent evt) {

        System.out.println("CLICK!");
        /* PSEUDOCODE
        if(a1 from Test1.isVisible()==true) {
           a1 from Test1.setVisible(false);
        } else {
            a1 from Test1.setVisible(true);
        }
        */
    }

}

如何从ButtonAssetListener检索a1实例以切换其可见性? 有没有更好的选择来在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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM