首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在框架java中水平划分三个相等的部分

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

Stack Overflow用户
提问于 2017-11-10 06:13:15
回答 1查看 3.3K关注 0票数 1

我用GridBagConstraints,在这里输入图像描述将图像分割成三部分:粉红色背景的水平部分和黄色和蓝色背景下的垂直部分。

我用下面的代码来做这个,

代码语言:javascript
复制
 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部分,黄色背景分成两部分。我试过这样做。但这不适合我。我不想用分隔板来做这件事。是否可以使用GridBagConstraints来实现它?你能给我提个主意吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-10 07:34:47

使用GridLayout实现这一点的简单方法是将contentPane划分为两行,然后将底部的行划分为两列。

代码语言:javascript
复制
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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47216673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档