jpanel没有很好地显示jframe设置为gridbaglayout

时间:2012-03-10 09:46:38

标签: java swing jpanel layout-manager gridbaglayout

下面的程序是使用gridbaglayout将jpanel定位在jframe的左上角,而是在jframe的中心显示一个非常小的框。当我将jframe的布局设置为null时,jpanel显示正常。谁能告诉我为什么jpanel被压缩到framebaglayout的框架中心?我真的需要使用gridbag。请帮忙

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

public class Major {

    //defining the constructor
    public Major() {
        JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
        JPanel headPanel = new JPanel(); //creating the header panel
        maFrame.setSize(900, 700); //setting size
        maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame
        Container container = maFrame.getContentPane();
        container.setLayout(new GridBagLayout()); //setting layout of main frame
        GridBagConstraints cns = new GridBagConstraints(); //creating constraint
        cns.gridx = 0;
        cns.gridy = 0;
        maFrame.setLocationRelativeTo(null); //centering frame
        headPanel.setBackground(Color.WHITE);
        headPanel.setSize(200, 150);
        container.add(headPanel, cns);
        maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
        maFrame.setVisible(true); //making the frame visible
    }

//defining the main method
    public static void main(String[] args) {
        new Major(); //instantiating the class
    }
}

2 个答案:

答案 0 :(得分:6)

似乎您忘记向weightx提供weightyGridBagConstraints限制,正如您提供的那样,您将看到您的JPanel。

在这里,我使用这些约束修改了代码。

并且永远不要使用这一行,headPanel.setSize(200, 150);,因为我已经评论过了,因为我提到的限制将为你解决这个问题。

使用图片添加新代码

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

 public class Major 
 {


    //defining the constructor
    public Major() 
    {

        JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
        JPanel headPanel = new JPanel(); //creating the header panel
        maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame
        Container container = maFrame.getContentPane();
        container.setLayout(new GridBagLayout()); //setting layout of main frame
        GridBagConstraints cns = new GridBagConstraints(); //creating constraint
        cns.gridx = 0;
        cns.gridy = 0;
        //cns.gridwidth = 3;
        //cns.gridheight = 4;
        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.WHITE);
        container.add(headPanel, cns);

        JPanel panel = new JPanel();
        panel.setBackground(Color.BLUE);
        cns.gridx = 1;
        cns.gridy = 0;
        //cns.gridwidth = 7;
        //cns.gridheight = 4;
        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.DARK_GRAY);
        cns.gridx = 0;
        cns.gridy = 1;
        cns.gridwidth = 2;
        //cns.gridheight = 4;
        cns.weightx = 1.0;
        cns.weighty = 0.3;
        cns.anchor = GridBagConstraints.LAST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel1, cns);     

        //JButton button = new JButton("BUTTON");
        //headPanel.add(button);

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

    //defining the main method
    public static void main(String[] args) 
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                new Major(); //instantiating the class
            }
        };
        SwingUtilities.invokeLater(runnable);       
    }
}

这是输出:

GRIDBAGLAYOUT

答案 1 :(得分:4)

您必须将至少一个GridBagConstraint的weightxweighty设置为大于0.0的某个值!

如果整个布局小于可用空间,则权重属性用于指示额外空间发生的情况。如果所有权重(对于一个方向)为零(默认值),则整个布局居中。如果至少一个权重大于零,则额外空间按其权重的比例分配到列或行,因此布局将占用所有可用空间。