Java jframe中的多个JPanel

Java jframe中的多个JPanel,java,swing,jframe,jpanel,windowbuilder,Java,Swing,Jframe,Jpanel,Windowbuilder,我目前正在尝试使用一个有多个JPanel的jframe制作一个小应用程序。我有几个问题要问 必须有一种更干净的方法来制作一个包含16个不同面板的应用程序,而不是将其全部放在一个类中。还有其他选择吗 目前我只有3个面板。我没有进一步讨论,因为其中两个面板没有反映我的更改。它们是我使用的两个面板 必须有一种更干净的方法来制作一个包含16个不同面板的应用程序,而不是将其全部放在一个类中。还有其他选择吗 您可以根据需要自由创建和使用任意多的类。因此,如果一个JPanel拥有一个复杂的GUI,您可能希望在

我目前正在尝试使用一个有多个JPanel的jframe制作一个小应用程序。我有几个问题要问

  • 必须有一种更干净的方法来制作一个包含16个不同面板的应用程序,而不是将其全部放在一个类中。还有其他选择吗

  • 目前我只有3个面板。我没有进一步讨论,因为其中两个面板没有反映我的更改。它们是我使用的两个面板

  • 必须有一种更干净的方法来制作一个包含16个不同面板的应用程序,而不是将其全部放在一个类中。还有其他选择吗

    您可以根据需要自由创建和使用任意多的类。因此,如果一个JPanel拥有一个复杂的GUI,您可能希望在其他地方重用它,或者它有自己的特定和独立的功能,那么一定要将代码放在它自己的类中

    目前我只有3个面板。我没有进一步讨论,因为其中两个面板没有反映我的更改。它们是我使用的两个面板

    闻起来像是你在试图重新发明卡片布局。既然你可以直接使用它,为什么还要重新发明呢


    是的,程序员所说的关于空布局的一切都是真的。您应该避免使用它。

    您的错误是使用了
    null
    布局、
    revalidate
    invalidate
    validate
    将不再具有任何重要意义,因为它们与支持布局管理API相关

    因为您已删除布局管理器,所以面板不再有任何内容可以告诉它们应该显示在什么大小或位置,这意味着当您添加新组件时,它的大小为0x0,位置为0x0

    使用示例更新

    您应该利用布局管理器API的原因有很多,包括自动处理不同系统上字体呈现方式的差异、动态和可调整大小的布局、屏幕分辨率和DPI的差异等等

    它还将鼓励您将UI划分为多个责任区,而不是试图将整个UI代码转储到一个类中(是的,我看到了这一点,是的,我职业生涯的大部分时间都在清理那些这样做的人……)

    本例使用and,但您应该花时间使用


    哦,卡片布局可能适合你。我考虑过使用JTabbedPane。以下是我在代码示例中的想法:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    
    
    public class TabbedPaneDemo extends JFrame {
    
        public TabbedPaneDemo() {
    
            // set the layout of the frame to all the addition of all components
            setLayout(new FlowLayout());
    
            // create a tabbed pane
            JTabbedPane tabbedPane = new JTabbedPane();
            tabbedPane.setPreferredSize(new Dimension(500,500));
            add(tabbedPane);
    
            // create three panels to be added to this frame
            JPanel redPanel = new JPanel();
            JPanel greenPanel = new JPanel();
            JPanel bluePanel = new JPanel();
    
            // set the colors of the panels
            redPanel.setBackground(Color.RED);
            greenPanel.setBackground(Color.GREEN);
            bluePanel.setBackground(Color.BLUE);
    
            // set the preferred size of each panel
            redPanel.setPreferredSize(new Dimension(150,150));
            greenPanel.setPreferredSize(new Dimension(150,150));
            bluePanel.setPreferredSize(new Dimension(150,150));
    
            // add the panels to the tabbed pane
            tabbedPane.addTab("Red Panel", redPanel);
            tabbedPane.addTab("Green Panel", greenPanel);
            tabbedPane.addTab("Blue Panel", bluePanel);
    
            // finish initializing this window
            setSize(500,500); // size the window to fit its components (i.e. panels in this case)
            setLocationRelativeTo(null); // center this window
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit application when this window is closed
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TabbedPaneDemo().setVisible(true);
                }
            });
        }
    
    }
    

    关于你的另一个问题,关于在一节课上有16个面板:

  • 您可以在面板中使用面板来组织事物
  • 您可以将JPanel子类化,并将子类放入它们自己的.java文件中

  • 如果我能提供进一步帮助,你可以给我发封电子邮件。kaydell@yahoo.com(我喜欢帮助人们进行编程,我也从中学习。)

    第一个错误是使用
    null
    布局、
    revalidate
    invalidate
    validate
    将不再具有任何重要意义,因为它们与支持布局管理相关感谢您的帮助,切换布局修复了问题!也很感谢你的回答有多彻底。(本应为+1,但需要15次重复)布局管理器可能看起来令人困惑,但从长远来看,它们将为您节省大量时间和工作;)
    public class Jframetest extends JFrame {
    
        private JPanel Home;
        private JPanel masslog;
        private JPanel DEH;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Jframetest frame = new Jframetest();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        public Jframetest() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            setBounds(100, 100, 618, 373);
            Home = new JPanel();
            masslog = new JPanel();
            DEH = new JPanel();
    
            Home.setBackground(new Color(255, 250, 250));
            Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
            DEH.setBackground(new Color(255, 250, 250));
            DEH.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
            masslog.setBackground(new Color(255, 250, 250));
            masslog.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
            setContentPane(Home);
            Home.setLayout(null);
    
            JButton dehbutton = new JButton("Sign in");
            dehbutton.setFont(new Font("Tahoma", Font.PLAIN, 14));
            dehbutton.setForeground(new Color(0, 0, 0));
            dehbutton.setBackground(UIManager.getColor("Menu.selectionBackground"));
            DEH.add(dehbutton);
    
            JButton btnNewButton = new JButton("Data Entry login");
            btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
            btnNewButton.setForeground(new Color(0, 0, 0));
            btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground"));
    
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    Home.removeAll();
                    Home.add(DEH);
                    Home.revalidate();
                    Home.repaint();
    
                    // JOptionPane.showMessageDialog(null, "Username/Password incorrect");
                }
            });
            btnNewButton.setBounds(44, 214, 204, 61);
            Home.add(btnNewButton);
    
            final JButton button = new JButton("Manager and Associate login");
            button.setFont(new Font("Tahoma", Font.PLAIN, 14));
            button.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Home.removeAll();
                    Home.add(masslog);
                    Home.revalidate();
                    Home.repaint();
    
                }
            });
            button.setBounds(340, 214, 204, 61);
            Home.add(button);
    
            JTextPane txtpnEmployeeLogin = new JTextPane();
            txtpnEmployeeLogin.setForeground(Color.DARK_GRAY);
            txtpnEmployeeLogin.setBackground(Color.WHITE);
            txtpnEmployeeLogin.setFont(new Font("Tahoma", Font.PLAIN, 34));
            txtpnEmployeeLogin.setText("Employee Login");
            txtpnEmployeeLogin.setBounds(181, 123, 260, 52);
            Home.add(txtpnEmployeeLogin);
    
            JLabel lblNewLabel = new JLabel("New label");
            lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Will and April\\Downloads\\your-logo-here.jpg"));
            lblNewLabel.setBounds(427, 11, 165, 67);
            Home.add(lblNewLabel);
    
        }
    
    }
    
    removeAll();
    add();
    revalidate();
    repaint(); 
    
    import java.awt.CardLayout;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    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.JTextField;
    import javax.swing.border.EmptyBorder;
    
    public class FrameTest extends JFrame {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        FrameTest frame = new FrameTest();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        public FrameTest() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            final CardLayout layout = new CardLayout();
            setLayout(layout);
            LoginPane loginPane = new LoginPane();
            add(loginPane, "login");
            add(new NewLoginPane(), "newLogin");
            add(new ManagerLoginPane(), "managerLogin");
    
            loginPane.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String command = e.getActionCommand();
                    System.out.println(command);
                    if ("new".equals(command)) {
                        layout.show(getContentPane(), "newLogin");
                    } else if ("manager".equals(command)) {
                        layout.show(getContentPane(), "managerLogin");
                    }
                }
            });
    
            layout.show(getContentPane(), "layout");
    
            pack();
            setLocationRelativeTo(null);
        }
    
        public class LoginPane extends JPanel {
    
            private JTextField userName;
            private JButton newButton;
            private JButton managerButton;
    
            public LoginPane() {
                setBorder(new EmptyBorder(20, 20, 20, 20));
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = 2;
                gbc.weightx = 1;
                gbc.insets = new Insets(10, 10, 10, 10);
    
                userName = new JTextField(10);
                userName.setFont(new Font("Tahoma", Font.PLAIN, 34));
                add(userName, gbc);
    
                gbc.gridx = 0;
                gbc.gridy = 1;
                gbc.gridwidth = 1;
                gbc.weightx = 0;
                gbc.fill = GridBagConstraints.HORIZONTAL;
    
                newButton = new JButton("Sign in");
                newButton.setActionCommand("new");
                managerButton = new JButton("Manager and Associate login");
                managerButton.setActionCommand("manager");
    
                add(newButton, gbc);
                gbc.gridx++;
                add(managerButton, gbc);                
            }
    
            public void addActionListener(ActionListener listener) {
                newButton.addActionListener(listener);
                managerButton.addActionListener(listener);
            }
    
            public void remveActionListener(ActionListener listener) {
                newButton.removeActionListener(listener);
                managerButton.removeActionListener(listener);
            }
    
            public String getUserName() {               
                return userName.getText();                
            }            
        }
    
        public class NewLoginPane extends JPanel {
            public NewLoginPane() {
                setLayout(new GridBagLayout());
                add(new JLabel("New Login"));
            }           
        }
    
        public class ManagerLoginPane extends JPanel {
            public ManagerLoginPane() {
                setLayout(new GridBagLayout());
                add(new JLabel("Welcome overlord"));
            }           
        }
    }
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    
    
    public class TabbedPaneDemo extends JFrame {
    
        public TabbedPaneDemo() {
    
            // set the layout of the frame to all the addition of all components
            setLayout(new FlowLayout());
    
            // create a tabbed pane
            JTabbedPane tabbedPane = new JTabbedPane();
            tabbedPane.setPreferredSize(new Dimension(500,500));
            add(tabbedPane);
    
            // create three panels to be added to this frame
            JPanel redPanel = new JPanel();
            JPanel greenPanel = new JPanel();
            JPanel bluePanel = new JPanel();
    
            // set the colors of the panels
            redPanel.setBackground(Color.RED);
            greenPanel.setBackground(Color.GREEN);
            bluePanel.setBackground(Color.BLUE);
    
            // set the preferred size of each panel
            redPanel.setPreferredSize(new Dimension(150,150));
            greenPanel.setPreferredSize(new Dimension(150,150));
            bluePanel.setPreferredSize(new Dimension(150,150));
    
            // add the panels to the tabbed pane
            tabbedPane.addTab("Red Panel", redPanel);
            tabbedPane.addTab("Green Panel", greenPanel);
            tabbedPane.addTab("Blue Panel", bluePanel);
    
            // finish initializing this window
            setSize(500,500); // size the window to fit its components (i.e. panels in this case)
            setLocationRelativeTo(null); // center this window
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit application when this window is closed
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TabbedPaneDemo().setVisible(true);
                }
            });
        }
    
    }