下面的程序是使用 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
}
}
最佳答案
您似乎忘记为您的 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);
}
}
这里是输出:
关于java - jpanel 无法很好地显示 jframe 设置为 gridbaglayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645463/