在JFrame上排列JPanel

时间:2019-06-18 11:34:38

标签: java swing

我有一个框架(600X500),一个JPanel(50X100)和另一个JPanel(200X150),并且
我正在尝试获得以下结果:

enter image description here

我的代码是:

public class BtnsPanel extends JPanel{
    public BtnsPanel()
    {
        setSize(50,100);
        setBackground(Color.RED);
    }
}
public class DialogPanel extends JPanel{
    public DialogPanel() {
        setSize(150,150);
        setBackground(Color.BLUE);
    }
}
public class MainFrame extends JFrame{  
    public MainFrame()
    {
        setSize(600,500);
        setLayout(new BorderLayout());
        add(new BtnsPanel(), BorderLayout.CENTER);
        add(new DialogPanel(), BorderLayout.CENTER);        
    }

    public static void main(String[] args){                
                new MainFrame().setVisible(true);                   
    }
}

代码结果:

enter image description here

那不是预期的结果。

2 个答案:

答案 0 :(得分:5)

您的图片未按比例绘制,但是如果您未尝试达到所提供尺寸的确切比例,我建议您使用GridBagLayout而不是BorderLayout

public class MainFrame extends JFrame {

    private JPanel btnsPanel;
    private JPanel dialogPanel;

    public MainFrame() {
        getContentPane().setBackground(Color.BLUE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setPreferredSize(new Dimension(600,500));

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.2, 0.1, 0.5, 0.2};
        gridBagLayout.rowWeights = new double[]{0.3, 0.3, 0.15, 0.25};
        getContentPane().setLayout(gridBagLayout);

        btnsPanel = new JPanel();
        btnsPanel.setBackground(Color.RED);
        GridBagConstraints gbc_btnsPanel = new GridBagConstraints();
        gbc_btnsPanel.insets = new Insets(0, 0, 5, 5);
        gbc_btnsPanel.fill = GridBagConstraints.BOTH;
        gbc_btnsPanel.gridx = 0;
        gbc_btnsPanel.gridy = 0;
        gbc_btnsPanel.gridheight = 2;
        getContentPane().add(btnsPanel, gbc_btnsPanel);

        dialogPanel = new JPanel();
        dialogPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
        dialogPanel.setBackground(Color.BLUE);
        GridBagConstraints gbc_dialogPanel = new GridBagConstraints();
        gbc_dialogPanel.insets = new Insets(0, 0, 0, 5);
        gbc_dialogPanel.fill = GridBagConstraints.BOTH;
        gbc_dialogPanel.gridx = 2;
        gbc_dialogPanel.gridy = 1;
        gbc_dialogPanel.gridheight = 2;
        getContentPane().add(dialogPanel, gbc_dialogPanel);
        pack();
    }
}

代码结果:

This would be reflecting the look of the picture you've provided. (border of DialogPanel is just for reference)

答案 1 :(得分:3)

使用EvT提出的GridbagLayout是一个很好且有效的解决方案。
但是,如果您想避免GridBagConstraints的复杂性,则可以通过将btnsPaneldialogPanel分别包装成JPanel来实现类似的结果:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MainFrame extends JFrame {

    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cPane = getContentPane();
        cPane.setBackground(Color.BLUE);
        cPane.setLayout(new BorderLayout());

        JPanel leftPane = new JPanel();
        leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.Y_AXIS));
        leftPane.setOpaque(false);

        JPanel btnsPanel = new JPanel();
        btnsPanel.setBackground(Color.RED);
        btnsPanel.setPreferredSize(new Dimension(50,100));

        leftPane.add(btnsPanel);
        leftPane.add(Box.createVerticalGlue());
        cPane.add(leftPane, BorderLayout.LINE_START);

        JPanel centerPane = new JPanel(new GridBagLayout());
        centerPane.setPreferredSize(new Dimension(500,500));
        centerPane.setOpaque(false);

        JPanel dialogPanel = new JPanel();
        dialogPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
        dialogPanel.setBackground(Color.BLUE);
        dialogPanel.setPreferredSize(new Dimension(200,150));
        centerPane.add(dialogPanel);

        cPane.add(centerPane, BorderLayout.CENTER);
        pack();
    }

    public static void main(String[] args){
        new MainFrame().setVisible(true);
    }
}

enter image description here