我可以连续放置 5 个面板吗

Can I have 5 panels in a row

是否可以在单个帧中连续显示 5 JPanels?这是我的代码,您可能会更好地理解这个问题:

setLayout(new BorderLayout());
add(mainOrderInfo, BorderLayout.WEST);
add(meatPanel, BorderLayout.WEST);
add(cheesePanel, BorderLayout.CENTER);
add(ingrediantsPanel, BorderLayout.EAST);
add(addonsPanel, BorderLayout.EAST);

当代码为 运行 时,我得到的只有 meatPanelcheesePanelingrediantsPanel。我希望 mainOrderInfomeatPanel 的左边,addonsPanelingredientsPanel 的右边。我觉得这只是我不知道的代码或代码顺序问题。

您可以使用 BoxLayout.

布局将多个面板排成一行

The Swing packages include a general purpose layout manager named BoxLayout. BoxLayout either stacks its components on top of each other or places them in a row — your choice. You might think of it as a version of FlowLayout, but with greater functionality.

对于您的面板(ingrediantsPanel、addonsPanel),将所需的面板放在 Box box = new Box(BoxLayout.X_AXIS); 并将此 box 添加到 EAST。面板将沿 x 轴布置。

GridLayout

有关详细信息,请参阅 How to Use GridLayout

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(1, 5));
            add(createPanel(Color.WHITE));
            add(createPanel(Color.BLACK));
            add(createPanel(Color.RED));
            add(createPanel(Color.GREEN));
            add(createPanel(Color.BLUE));
        }

        protected JPanel createPanel(Color color) {
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, 50);
                }
            };
            panel.setBackground(color);
            return panel;
        }

    }

}

GridBagLayout

有关详细信息,请参阅 How to Use GridBagLayout

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(createPanel(Color.WHITE), gbc);
            gbc.gridx++;
            add(createPanel(Color.BLACK), gbc);
            gbc.gridx++;
            add(createPanel(Color.RED), gbc);
            gbc.gridx++;
            add(createPanel(Color.GREEN), gbc);
            gbc.gridx++;
            add(createPanel(Color.BLUE), gbc);
        }

        protected JPanel createPanel(Color color) {
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, 50);
                }
            };
            panel.setBackground(color);
            return panel;
        }

    }

}

这应该可以解决您的问题

setLayout(new GridLayout(1,5));

add(mainOrderInfo);

add(meatPanel);

add(cheesePanel);

add(ingrediantsPanel);

add(addonsPanel;