且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JFrame 中的 JPanel 之间切换

更新时间:2023-12-05 18:16:34

我需要在 if 块中放入什么来更改面板视图?我会假设 jPanel1.something() 但我不知道那是什么."

"What do I need to put inside the if blocks to change panel views? I would have assumed jPanel1.something() but I don't know what that something is."

  1. 不要将字符串与 == 进行比较,它不会起作用.使用 .equals.. if("runserver".equals(str)){

  1. Don't compare string with ==, it will not work. Use .equals.. if("runserver".equals(str)){

您需要使用 CardLayout

CardLayout.show(jPanel1, "whichPanel");

  • public void show(Container parent, String name) - 使用 addLayoutComponent 翻转到使用指定名称添加到此布局的组件.如果不存在这样的组件,则不会发生任何事情.

    • public void show(Container parent, String name) - Flips to the component that was added to this layout with the specified name, using addLayoutComponent. If no such component exists, then nothing happens.

      void setPanel(String str){
          CardLayout layout = (CardLayout)jPanel1.getLayout();
          if("runserver".equals(str)){
              layout.show(jPanel1, "card4");
      
          }else if("connectserver".equals(str)){
             layout.show(jPanel1, "card3");
      
          } else{
              layout.show(jPanel1, "card2");
          }
      }
      

    • 请参阅如何使用 CardLayout 了解更多详情,请参阅 API 了解更多方法.


      See How to Use CardLayout for more details and see the API for more methods.

      更新

      尝试运行此示例并使用您的代码检查它,看看您是否发现有任何帮助

      Try running this example and examine it with your code to see if you notice anything that will help

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class TestCardLayout {
    
        PanelOne p1 = new PanelOne();
        PanelTwo p2 = new PanelTwo();
        PanelThree p3 = new PanelThree();
    
        CardLayout layout = new CardLayout();
        JPanel cardPanel = new JPanel(layout);
    
        public TestCardLayout() {
            JButton showOne = new JButton("Show One");
            JButton showTwo = new JButton("Show Two");
            JButton showThree = new JButton("Show Trree");
            JPanel buttonsPanel = new JPanel();
            buttonsPanel.add(showOne);
            buttonsPanel.add(showTwo);
            buttonsPanel.add(showThree);
            showOne.addActionListener(new ButtonListener());
            showTwo.addActionListener(new ButtonListener());
            showThree.addActionListener(new ButtonListener());
    
            cardPanel.add(p1, "panel 1");
            cardPanel.add(p2, "panel 2");
            cardPanel.add(p3, "panel 3");
    
            JFrame frame = new JFrame("Test Card");
            frame.add(cardPanel);
            frame.add(buttonsPanel, BorderLayout.SOUTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        private class ButtonListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                String command = e.getActionCommand();
                if ("Show One".equals(command)) {
                    layout.show(cardPanel, "panel 1");
                } else if ("Show Two".equals(command)) {
                    layout.show(cardPanel, "panel 2");
                } else {
                    layout.show(cardPanel, "panel 3");
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestCardLayout testCardLayout = new TestCardLayout();
                }
            });
        }
    }
    
    class PanelOne extends JPanel {
    
        public PanelOne() {
            setBackground(Color.GREEN);
            add(new JLabel("Panel one"));
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
    
    class PanelTwo extends JPanel {
    
        public PanelTwo() {
            setBackground(Color.BLUE);
            add(new JLabel("Panel two"));
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
    
    class PanelThree extends JPanel {
    
        public PanelThree() {
            setBackground(Color.YELLOW);
            add(new JLabel("Panel three"));
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
    

    更新 2

    问题是,按钮在 ConfigurePanel 类中.尝试在该类中创建新的 StartUPGUI 不会引用相同的组件.您需要做的是将 StartUPGUI 的引用传递给 ConfigurePanel.像这样的

    The problem is, the the button is in the ConfigurePanel class. Trying to create a new StartUPGUI in that class, won't reference the same components. What you need to do is pass a reference of the StartUPGUI to the ConfigurePanel. Something like this

        public class ConfigurePanel extends JPanel {
            StartUPGUI startup;
    
            public ConfigurePanel(StartUPGUI startup) {
                this.startup = startup;
            }
    
            ....
            public void actionPerformed(ActionEvent e) {
                startup.setPanel("runserver");
    
            }
        }
    

    并像这样从 StartUPGUI 实例化 ConfigurePanel

    And instantiate ConfigurePanel from the StartUPGUI like this

        new ConfigurePanel(StartUPGUI.this);