首页 > 解决方案 > 单击一个按钮打开一个新的 JFrame/GUI,

问题描述

我似乎无法找到让我的代码工作的方法。我正在制作一个程序,直到现在一切正常,我有一些按钮,他们做他们应该做的。但是现在我添加了一个按钮,当用户单击它时,它应该关闭当前的 GUI 并打开一个新的。我还想指出,我为这个新的 GUI 创建了一个新类。

我要调用的另一个 GUI 类是GuiCrafting,在该类中,GUI 也是全部编码的,如果我在Main上调用它就可以工作。

我的问题是我在这里输入什么(我尝试了很多东西,比如 dispose() 等,但我只是收到错误消息):

  public void actionPerformed(ActionEvent event) {
     
        if( str.equals("Crafting")){


        //insert code to call the GuiCrafting class and open his GUI



        }

在此先感谢您,如果您需要更多信息,请告诉我。

标签: javaswingjframe

解决方案


多个JFrames 不受欢迎,您可以在此处此处阅读

也许您想要使用的是CardLayoutJPanel ,它管理共享相同显示空间的两个或多个组件(通常是实例)。

在此处输入图像描述

点击“Goto Card 2”按钮后

在此处输入图像描述

测试应用程序.java:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestApp {

    final static String CARD1 = "Card1";
    final static String CARD2 = "Card2";
    
    public TestApp() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestApp::new);
    }

    private void initComponents() {
        JFrame frame = new JFrame("TestApp");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // create the panel that contains the "cards".
        JPanel cards = new JPanel(new CardLayout());

        // card 1 components
        JButton buttonGotoCard2 = new JButton("Goto Card 2");
        buttonGotoCard2.addActionListener((ActionEvent e) -> {
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, CARD2);
        });
        // create card 1
        JPanel card1 = new JPanel();
        card1.add(new JLabel("Card 1"));
        card1.add(buttonGotoCard2);

        // card 2 components
        JButton buttonGotoCard1 = new JButton("Goto Card 1");
        buttonGotoCard1.addActionListener((ActionEvent e) -> {
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, CARD1);
        });
        // create card 2
        JPanel card2 = new JPanel();
        card2.add(new JLabel("Card 2"));
        card2.add(buttonGotoCard1);

        // add cards to cards panel
        cards.add(card1, CARD1);
        cards.add(card2, CARD2);

        frame.getContentPane().add(cards, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

}

还有一个JDialog可能是您想要的。

然而

你可以很容易地做这样的事情(如果你必须JFrame从另一个打开一个):

测试应用程序.java:

import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class TestApp {

    public TestApp() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestApp::new);
    }

    private void initComponents() {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));

        JLabel label = new JLabel("JFrame 1");
        JButton button = new JButton("Open JFrame 2");

        button.addActionListener((ActionEvent e) -> {
            this.showNewJFrame(new WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    // here we listen for the second JFrame being closed so we can bring back the main JFrame
                    mainFrame.setVisible(true);
                }
            });

            // hide the main JFrame
            mainFrame.setVisible(false);
        });

        panel.add(label);
        panel.add(button);

        mainFrame.add(panel);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    private void showNewJFrame(WindowAdapter windowAdapter) {
        JFrame frame2 = new JFrame();
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // we dont wnat to exit when this JFrame is closed
        
        JPanel panel2 = new JPanel();
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        panel2.setBorder(new EmptyBorder(10, 10, 10, 10));
        
        JLabel label2 = new JLabel("JFrame 2");
        
        panel2.add(label2);
        frame2.add(panel2);

        frame2.addWindowListener(windowAdapter);
        frame2.pack();
        frame2.setVisible(true);
    }
}

这会产生:

在此处输入图像描述

当点击“Open JFrame 2”时:

在此处输入图像描述

JFrame 2关闭时,它会JFrame通过WindowAdapter#windowClosing.


推荐阅读