在 Java 中单击按钮时如何返回上一个 GUI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30861139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:45:42  来源:igfitidea点击:

How to go back to the previous GUI when a button is clicked in Java

javaswinguser-interface

提问by sara

I have a Java application that should display Gui1 then go to Gui 2 which I successful did , Then from Gui2 go back to Gui 1 which I have problems in doing. So how can I go back to Gui 1 when I press a button in Gui2

我有一个 Java 应用程序应该显示 Gui1,然后转到我成功执行的 Gui 2,然后从 Gui2 返回到 Gui 1,我在做时遇到了问题。那么当我在 Gui2 中按下一个按钮时我怎么能回到 Gui 1

Gui1 code

     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.JButton;
     import javax.swing.JFrame;
     import javax.swing.SwingUtilities;
     public class Gui1 extends JFrame {   
     private JButton btn=new JButton("Open Gui2");

    public Gui1()
    {
        super(" GUI1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        btn.addActionListener(new ButtonListener());
        btn.setActionCommand("Open");
        add(btn);
         }
 class ButtonListener implements ActionListener {
         public void actionPerformed(ActionEvent e)
            {
                String cmd = e.getActionCommand();

                if(cmd.equals("Open"))
                {
                    dispose();

                    new Gui2();
                }
            } 
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable(){


            public void run()
            {
                new Gui1().setVisible(true);
            }

        });
    }
}

Gui2 code

桂2码

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

public class Gui2 extends JFrame
{
    private JButton btn1= new JButton("Go Back to Gui 1");
    public Gui2()
    {
        super("Another GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add(btn1);

        setVisible(true);
    }
}

回答by Rafiq

Go back previous GUI you can use bellow code:

返回之前的 GUI,您可以使用以下代码:

  public class Gui2 extends JFrame {
 private JButton btn1 = new JButton("Go Back to Gui 1");

public Gui2() {
    super("Another GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(btn1);
    btn1.addActionListener(new Gui2.ButtonListener());
    btn1.setActionCommand("back");
    setVisible(true);
}

private class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("back")) {
            dispose();

            new Gui1().setVisible(true);
        }
    }
}

}

}

回答by kama_kazzi

Try only using one JFrame by making Gui1 and Gui2 panels. You can easily switch between them by getting rid of the first panel then adding the second.

通过制作 Gui1 和 Gui2 面板,尝试仅使用一个 JFrame。通过去掉第一个面板然后添加第二个面板,您可以轻松地在它们之间切换。

      getContentPane().removeAll(); //gets rid of first panel
      getContentPane().add(panel); //adds desired panel to frame
      validate(); //updates frame with new panel

回答by Sw4Tish

To achieve this, you could try to pass the reference of your Gui1class into the constructor of the Gui2class and create a private attribute which store your first window. Then just create a button that implements ActionListenerand hide/show the desired window.

为此,您可以尝试将Gui1类的引用传递到类的构造函数中,Gui2并创建一个私有属性来存储您的第一个窗口。然后只需创建一个按钮来实现ActionListener和隐藏/显示所需的窗口。

回答by dly

Use the ButtonListenerfor both classes. There ist still room for improvements, but this is a solution based on your code, that works. The ButtonListenernow takes the calling JFrameas argument to close it when needed.

ButtonListener对两个类都使用。仍有改进的空间,但这是基于您的代码的解决方案,有效。在ButtonListener现在只需调用JFrame的参数在需要的时候将其关闭。

I also changed the location of setVisible(true);from the main() to both constructors.

我还将setVisible(true);main()的位置更改为两个构造函数。

Gui1

桂1

public class Gui1 extends JFrame {
    private JButton btn = new JButton("Open Gui2");

    public Gui1() {
        super(" GUI1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(new ButtonListener(this));
        btn.setActionCommand("Open");
        add(btn);

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Gui1();
            }

        });
    }
}

Gui2

桂2

public class Gui2 extends JFrame {
    private JButton btn1 = new JButton("Go Back to Gui 1");

    public Gui2() {
        super("Another GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn1.addActionListener(new ButtonListener(this));
        btn1.setActionCommand("Open");
        add(btn1);

        setVisible(true);
    }
}

ButtonListener

按钮监听器

public class ButtonListener implements ActionListener {
    JFrame caller = null;

    public ButtonListener(JFrame caller) {
        this.caller = caller;
    }

    public ButtonListener() {}

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("Open")) {
            if (caller instanceof Gui1) {
                new Gui2();
            } else {
                new Gui1();
            }
            caller.dispose();
        }
    }
}