4

我希望能够拥有三个 JPanel p1 p2 和 p3,并让它们像这样布置:

布局
我一直在玩 FlowLayout、BoxLayout 等,但我不确定我是否朝着正确的方向前进。我对Java很陌生,所以如果我很诚实的话,我不知道会发生什么。

我喜欢 BoxLayout 的工作方式,调整面板的大小,但我希望能够给它某种宽度属性。

我没有为此使用视觉设计器,这是我目前的窗口代码:

private void initialize() {
    Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();

    frame = new JFrame();
    frame.setLayout(new GridLayout(1, 3));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
            dimensions.height / 2 - WINDOW_HEIGHT / 2, 
            WINDOW_WIDTH, WINDOW_HEIGHT);

    JPanel p1 = new JPanel(new BorderLayout());
    p1.setBackground(Color.red);

    JPanel p2 = new JPanel(new BorderLayout());
    p2.setBackground(Color.black);  

    JPanel p3 = new JPanel(new BorderLayout());
    p3.setBackground(Color.blue);       

    frame.add(p2);

    frame.add(p1);  
    frame.add(p3);
}

任何指针表示赞赏!

编辑:感谢 mKorbel,我已经设法让它按我想要的方式工作。右栏的布局与我打算做的不完全一样,但我实际上改变了主意并决定保留其他布局。

面板窗口
编码:

import java.awt.*;
import javax.swing.*;

public class PanelWindow extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final int WINDOW_HEIGHT = 600;
    private static final int WINDOW_WIDTH = 800;
    private JPanel p1, p2, p3;

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                PanelWindow panelWindow = new PanelWindow(); 
            }
        });
    }

    public PanelWindow() {
        initialize();
    }

    private void initialize() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        p1 = new JPanel();
        p1.setBackground(Color.red);
        p2 = new JPanel();
        p2.setBackground(Color.green);
        p3 = new JPanel();
        p3.setBackground(Color.blue);

        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 97;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(p1, gbc);

        gbc.gridy = 1;
        gbc.weightx = gbc.weighty = 3;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(p2, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.weightx = 20;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(p3, gbc);

        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
            dimensions.height / 2 - WINDOW_HEIGHT / 2, 
            WINDOW_WIDTH, WINDOW_HEIGHT);

        setTitle("Panel Window");
    }
}
4

2 回答 2

7

不是我最喜欢的LayoutManager,例如使用GridBagLayout,最简单的可能是使用MigLayout,也许......

在此处输入图像描述在此处输入图像描述

在此处输入图像描述

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        setLayout(new GridBagLayout());// set LayoutManager
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();

        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 70;
        add(panel1, gbc); // add compoenet to the COntentPane

        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
        gbc.gridy = 1;
        gbc.weightx = gbc.weighty = 30;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(panel2, gbc); // add component to the COntentPane

        JPanel panel3 = new JPanel();
        panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
        gbc.gridx =1;
        gbc.gridy = 0;
        gbc.gridwidth = /*gbc.gridheight = */1;
        gbc.gridheight = 2;
        gbc.weightx = /*gbc.weighty = */20;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(panel3, gbc);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
        pack();
        setVisible(true); // important
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() { // important

            public void run() {
                BorderPanels borderPanels = new BorderPanels();
            }
        });
    }
}
于 2012-06-29T11:40:11.497 回答
1

一个词:MigLayout。如您所见,使用标准布局管理器是可能的。但我可以向你保证,使用 MigLayout 布局代码将减少到 4 行...(尽管进入 MigLayout 需要一些时间,因为它的工作方式与通常的布局管理器有点不同)。

于 2012-06-29T13:40:44.157 回答