gpt4 book ai didi

java - 如何显示和隐藏现有的 JFrame

转载 作者:行者123 更新时间:2023-12-02 10:35:46 25 4
gpt4 key购买 nike

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

我有一个测试应用程序,由三个完全独立的窗口(JFrame)组成:

  1. 主菜单
  2. Assets 窗口 1
  3. Assets 窗口 2

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

这是启动所有窗口的主类:

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);

}

}

这是 Assets 窗口按钮 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);
}
}
}

关于java - 如何显示和隐藏现有的 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53306585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com