如何在java框架中水平划分三个相等的部分

如何在java框架中水平划分三个相等的部分,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我已经使用GridBagConstraints将帧分为三个部分,例如粉红色背景中的水平部分和黄色和蓝色背景中的垂直部分,就像在图像中一样 我正在使用下面的代码来执行此操作 public Main() { JFrame maFrame = new JFrame("The main screen"); //creating main Jframe maFrame.setSize(1000, 700); Contain

我已经使用GridBagConstraints将帧分为三个部分,例如粉红色背景中的水平部分和黄色和蓝色背景中的垂直部分,就像在图像中一样

我正在使用下面的代码来执行此操作

 public Main() 
       {
           JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
           maFrame.setSize(1000, 700);
           Container container = maFrame.getContentPane();
           container.setLayout(new GridBagLayout()); //setting layout of main frame
           GridBagConstraints cns = new GridBagConstraints(); //creating constraint

           JPanel headPanel = new JPanel(); //creating the header panel
           cns.gridx = 0;
           cns.gridy = 1;
           cns.weightx = 0.3;
           cns.weighty = 0.7;
           cns.anchor = GridBagConstraints.FIRST_LINE_START;
           cns.fill = GridBagConstraints.BOTH;
           maFrame.setLocationRelativeTo(null); //centering frame
           headPanel.setBackground(Color.YELLOW);
           container.add(headPanel, cns);

           JPanel panel = new JPanel();
           panel.setBackground(Color.BLUE);
           cns.gridx = 1;
           cns.gridy = 1;
           cns.weightx = 0.7;
           cns.weighty = 0.7;
           cns.anchor = GridBagConstraints.PAGE_START;
           cns.fill = GridBagConstraints.BOTH;
           container.add(panel, cns);

           JPanel panel1 = new JPanel();
           panel1.setBackground(Color.PINK);
           cns.gridx = 0;
           cns.gridy = 0;
           cns.gridwidth = 2;
           cns.weightx = 1.0;
           cns.weighty = 0.3;
           cns.anchor = GridBagConstraints.LAST_LINE_START;
           cns.fill = GridBagConstraints.BOTH;
           container.add(panel1, cns);     

           maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
           maFrame.pack();
           maFrame.setVisible(true); //making the frame visible
       }

我想把粉色背景部分分成3部分,黄色背景分为两部分。我已经试过了。但这对我不起作用。我不想使用splitpane来执行此操作。使用GridBagConstraints可以实现吗?你能给我一个建议吗?提前感谢。

使用
GridLayout
并将
contentPane
分为两行,然后再将底部的行分为两列

import javax.swing.*;
import java.awt.*;
class JpanelSplit {
    JFrame frame;
    JPanel contentPane;
    JPanel pinkPanel;
    JPanel yellowPanel;
    JPanel bluePanel;
    JPanel twoPanelContainer;

    public JpanelSplit() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel(new GridLayout(2,1));
        pinkPanel = new JPanel();
        pinkPanel.setBackground(Color.PINK);

        yellowPanel = new JPanel();
        yellowPanel.setBackground(Color.YELLOW);    

        bluePanel = new JPanel();
        bluePanel.setBackground(Color.BLUE);

        twoPanelContainer = new JPanel(new GridLayout(1,2));
        twoPanelContainer.add(yellowPanel);
        twoPanelContainer.add(bluePanel);

        contentPane.add(pinkPanel);
        contentPane.add(twoPanelContainer);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);

    }

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

你把粉红色背景分成三部分是什么意思?你需要图形分隔还是需要单独的3个JPanel?@aKilleR想要分隔为3个面板感谢你的回答。但是我想把粉红色的面板分成3列。如何实现?@Olive只需在粉色面板上设置一个布局,如
pinkPanel.setLayout(新的GridLayout(1,3))这将粉红色面板分成三列,感谢您的辛勤工作。我们能不能制作一个粉红色的面板是框架的1部分,两个面板容器是框架的3部分?我的意思是屏幕的三分之一,就像附加的图像一样,但这不是通过
GridLayout
实现的(因为GridLayout生成的网格大小相等),您可以在
contentPane
上使用
BorderLayout
,在
NORTH
twoPanelContainer
中添加粉色面板,我想这些东西能帮你做你想做的事。@Olive你得到答案了吗