【问题标题】:How to go back to the previous GUI when a button is clicked in Java在Java中单击按钮时如何返回上一个GUI
【发布时间】:2015-06-16 07:18:47
【问题描述】:

我有一个 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代码

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

【问题讨论】:

  • 必须保存Gui1 状态?
  • 顺便说一句:在两个框架中使用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 会在您设法关闭一个框架时引起不受欢迎的意外。 ;) 另外.. 使用逻辑一致的缩进代码行和块形式。缩进是为了让代码的流程更容易理解!

标签: java swing user-interface


【解决方案1】:

返回之前的 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);
        }
    }
}

}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      两个类都使用ButtonListener。 仍有改进的余地,但这是一个基于您的代码的解决方案,它有效。 ButtonListener 现在将调用 JFrame 作为参数以在需要时关闭它。

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

      Gui1

      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

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

      按钮监听器

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2020-01-20
          • 1970-01-01
          • 1970-01-01
          • 2021-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多