【问题标题】:Java - How to make contents of JFrame appear in the same Window instead of multiple windowsJava - 如何使 JFrame 的内容出现在同一个窗口而不是多个窗口中
【发布时间】:2014-03-15 14:09:32
【问题描述】:

我是 java 新手,正在进入高级水平,我在 GUI 控件中遇到问题,我制作了一个按钮,单击该按钮会打开一个新窗口,如下所示:

     JButton b = new JButton("Open New Window");
     b.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
     Window w = new Window();
     w.setVisible(true);
     }
     });

此窗口包含其他对象,但我一直在考虑以这样的方式制作按钮,而不是打开新的 JFrame,而是在同一个窗口中打开所有内容而不打开新窗口,老实说,我不知道该怎么做请问我可以得到一些专业的帮助

【问题讨论】:

    标签: java swing user-interface jframe


    【解决方案1】:

    我认为您需要针对这种情况的卡片布局。这里有一些代码可以为你指明正确的方向。

    class MyFrame extends JFrame {
    
        public MyFrame() {
    
            JComponent allMyStuff = new JComponent();
            JComponent allMyOtherStuff = new JComponent();
    
            this.getContentPane().setLayout(new CardLayout());
    
            this.getContentPane().add(allMyStuff, "1");
            this.getContentPane().add(allMyOtherStuff, "2");
    
            CardLayout cl = (CardLayout) (this.getContentPane().getLayout());
            cl.show(this.getContentPane(), "1");
    
            JButton b = new JButton("Open New Window"); //add somewhere to first compoonent
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
    
                    CardLayout cl = (CardLayout) (this.getContentPane().getLayout());
                    cl.show(this.getContentPane(), "2");
                }
            });
        }
    }
    

    我怀疑代码是否可以运行,但通常它包含这个想法。您在一个面板中有东西,在另一个面板中有东西,您只想在两者之间切换。当然需要在第一个面板(allMyStuff)的某个地方添加按钮。

    【讨论】:

      【解决方案2】:

      我不清楚当按下按钮时你想在 GUI 中显示什么,但也许你应该考虑创建不同的 JPanel“视图”并使用 CardLayout 在 GUI 中交换这些视图。

      例如,查看这些 StackOverflow 问题和答案:

      【讨论】:

        【解决方案3】:

        在您介绍的动作侦听器中,您可以访问实例变量。因此,您可以根据需要向您的 GUI 添加更多元素。我做了一个小演示,也许这就是你想做的。为了使您的 GUI 更好,您应该考虑使用布局管理器。

        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        
        public class GUI {
        
        JFrame frame; 
        JButton btn;
        JButton compToAdd;
        
        public GUI() {
            frame = new JFrame("Testwindow");
            frame.setSize(500, 500);
            frame.setLayout(null);
        
            btn = new JButton("test btn");
            btn.setBounds(20, 20, 200, 200);
        
        
            btn.addActionListener(new ActionListener() {
        
                @Override
                public void actionPerformed(ActionEvent e) {
                    compToAdd = new JButton("new Button");
                    compToAdd.setBounds(20, 220, 200, 200);
                    frame.add(compToAdd);
                    frame.repaint();
                }
            });
        
            frame.add(btn);
            frame.setVisible(true);
        }
        
        public static void main(String[] args) {
            GUI gui = new GUI();
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-30
          • 1970-01-01
          相关资源
          最近更新 更多