我可以连续5个面板吗?

时间:2015-08-13 04:51:26

标签: java jpanel border-layout

单个框架中是否可以连续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。我希望mainOrderInfo位于meatPanel左侧,addonsPanel位于ingredientsPanel右侧。我觉得它很简单,代码或代码排序我都不知道。

2 个答案:

答案 0 :(得分:1)

GridLayout

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

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

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

    }

}

答案 1 :(得分:1)

这应该可以解决您的问题

setLayout(new GridLayout(1,5));

add(mainOrderInfo);

add(meatPanel);

add(cheesePanel);

add(ingrediantsPanel);

add(addonsPanel;