4

以下のプログラムは、gridbaglayoutを使用してjframeの左上隅にjpanelを配置することですが、代わりに非常に小さなボックスがjframeの中央に表示されます。jframeのレイアウトをnullに設定すると、jpanelが正常に表示されます。jpanelがgridbaglayoutでフレームの中央に圧縮される理由を誰かに教えてもらえますか?私は本当に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
    }
}
4

2 に答える 2

6

あなたが提供するのを忘れたようです、weightxそしてweightyあなたへの制約GridBagConstraints、あなたがそれらを提供するとき、あなたはあなたの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

于 2012-03-10T10:00:25.523 に答える
4

weightx少なくとも1つのGridBagConstraintのとweightyを0.0より大きい値に設定 する必要があります。

重み属性は、レイアウト全体が使用可能なスペースよりも小さい場合に余分なスペースで何が起こるかを示すために使用されます。すべての重み(一方向)がデフォルト値のゼロの場合、レイアウト全体が中央に配置されます。少なくとも1つの重みがゼロより大きい場合、余分なスペースはその重みに比例して列または行に分散されるため、レイアウトは使用可能なすべてのスペースを占有します。

于 2012-03-10T10:24:45.133 に答える