[英]jpanel not displaying well with jframe set to 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
}
}
好像你忘了為你的GridBagConstraints
提供, weightx
和weighty
限制,當你提供它們時,你會看到你的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);
}
}
這是輸出:
您必須將至少一個GridBagConstraint的weightx
和weighty
設置為大於0.0的某個值!
如果整個布局小於可用空間,則權重屬性用於指示額外空間發生的情況。 如果所有權重(對於一個方向)為零(默認值),則整個布局居中。 如果至少一個權重大於零,則額外空間按其權重的比例分配到列或行,因此布局將占用所有可用空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.