Java GridBagLayout大小调整高度不正确?
此红色框应仅为屏幕高度的1/6。我根本无法使红盒子的高度降低。我如何确定所有这些的高度 谢谢Java GridBagLayout大小调整高度不正确?,java,swing,awt,gridbaglayout,Java,Swing,Awt,Gridbaglayout,此红色框应仅为屏幕高度的1/6。我根本无法使红盒子的高度降低。我如何确定所有这些的高度 谢谢 嗯,忽略这一点,它只是要求我提供更多关于代码的上下文,但问题是直截了当的。。。所以如果你喜欢lol这个包的名字,那就是我做的第一个包的名字。班上的一位朋友看到后笑得要死,所以从那以后就一直是这样。你的JFrame应该使用布局来规定相关面板的大小。然后,您可以通过调整GridBagLayout的公共字段来调整行和列的权重:列宽、行高、列权重和行权重 例如: import java.awt.EventQue
嗯,忽略这一点,它只是要求我提供更多关于代码的上下文,但问题是直截了当的。。。所以如果你喜欢lol这个包的名字,那就是我做的第一个包的名字。班上的一位朋友看到后笑得要死,所以从那以后就一直是这样。你的
JFrame
应该使用布局来规定相关面板的大小。然后,您可以通过调整GridBagLayout
的公共字段来调整行和列的权重:列宽
、行高
、列权重
和行权重
例如:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class GridBagLayoutExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutExample frame = new GridBagLayoutExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GridBagLayoutExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(450, 450));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, 6.0, 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, 6.0, 1.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JPanel top = new JPanel();
top.setBackground(Color.RED);
GridBagConstraints gbc_top = new GridBagConstraints();
gbc_top.gridwidth = 3;
gbc_top.insets = new Insets(0, 0, 5, 5);
gbc_top.fill = GridBagConstraints.BOTH;
gbc_top.gridx = 0;
gbc_top.gridy = 0;
contentPane.add(top, gbc_top);
JPanel left = new JPanel();
left.setBackground(Color.BLUE);
GridBagConstraints gbc_left = new GridBagConstraints();
gbc_left.insets = new Insets(0, 0, 5, 5);
gbc_left.fill = GridBagConstraints.BOTH;
gbc_left.gridx = 0;
gbc_left.gridy = 1;
contentPane.add(left, gbc_left);
JPanel middle = new JPanel();
middle.setBackground(Color.BLACK);
GridBagConstraints gbc_middle = new GridBagConstraints();
gbc_middle.insets = new Insets(0, 0, 5, 5);
gbc_middle.fill = GridBagConstraints.BOTH;
gbc_middle.gridx = 1;
gbc_middle.gridy = 1;
contentPane.add(middle, gbc_middle);
JPanel right = new JPanel();
right.setBackground(Color.YELLOW);
GridBagConstraints gbc_right = new GridBagConstraints();
gbc_right.insets = new Insets(0, 0, 5, 0);
gbc_right.fill = GridBagConstraints.BOTH;
gbc_right.gridx = 2;
gbc_right.gridy = 1;
contentPane.add(right, gbc_right);
JPanel bottom = new JPanel();
bottom.setBackground(Color.GREEN);
GridBagConstraints gbc_bottom = new GridBagConstraints();
gbc_bottom.insets = new Insets(0, 0, 0, 5);
gbc_bottom.gridwidth = 3;
gbc_bottom.fill = GridBagConstraints.BOTH;
gbc_bottom.gridx = 0;
gbc_bottom.gridy = 2;
contentPane.add(bottom, gbc_bottom);
pack();
}
}
无挫折(100100450300);可能很好code@mKorbel我已经更新为使用
setPreferredSize
和pack
。setPreferredSize是LayoutManagers API中实现的方法的专用作业,请改用JFrame.setSize(暂时不用),顺便说一句,欢迎来到这里。@mKorbel感谢您的建议。因此,由于JFrame
本身没有LayoutManager,setSize
比setPreferredSize
、setMinimumSize
更适合在使用LayoutManager时使用,对吗?这很有趣,因为您不需要直接设置MinMaxPreferredSize,但是LayoutManager需要此信息,如果需要,那么您必须为JComponent(尤其是Box/GBCLayout)重写此值,例如JScrollPane、JTable、JComboBox不会将合理的XxxSize返回到容器,这很有趣,但有很多例外(如其他地方)
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class GridBagLayoutExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutExample frame = new GridBagLayoutExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GridBagLayoutExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(450, 450));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, 6.0, 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, 6.0, 1.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JPanel top = new JPanel();
top.setBackground(Color.RED);
GridBagConstraints gbc_top = new GridBagConstraints();
gbc_top.gridwidth = 3;
gbc_top.insets = new Insets(0, 0, 5, 5);
gbc_top.fill = GridBagConstraints.BOTH;
gbc_top.gridx = 0;
gbc_top.gridy = 0;
contentPane.add(top, gbc_top);
JPanel left = new JPanel();
left.setBackground(Color.BLUE);
GridBagConstraints gbc_left = new GridBagConstraints();
gbc_left.insets = new Insets(0, 0, 5, 5);
gbc_left.fill = GridBagConstraints.BOTH;
gbc_left.gridx = 0;
gbc_left.gridy = 1;
contentPane.add(left, gbc_left);
JPanel middle = new JPanel();
middle.setBackground(Color.BLACK);
GridBagConstraints gbc_middle = new GridBagConstraints();
gbc_middle.insets = new Insets(0, 0, 5, 5);
gbc_middle.fill = GridBagConstraints.BOTH;
gbc_middle.gridx = 1;
gbc_middle.gridy = 1;
contentPane.add(middle, gbc_middle);
JPanel right = new JPanel();
right.setBackground(Color.YELLOW);
GridBagConstraints gbc_right = new GridBagConstraints();
gbc_right.insets = new Insets(0, 0, 5, 0);
gbc_right.fill = GridBagConstraints.BOTH;
gbc_right.gridx = 2;
gbc_right.gridy = 1;
contentPane.add(right, gbc_right);
JPanel bottom = new JPanel();
bottom.setBackground(Color.GREEN);
GridBagConstraints gbc_bottom = new GridBagConstraints();
gbc_bottom.insets = new Insets(0, 0, 0, 5);
gbc_bottom.gridwidth = 3;
gbc_bottom.fill = GridBagConstraints.BOTH;
gbc_bottom.gridx = 0;
gbc_bottom.gridy = 2;
contentPane.add(bottom, gbc_bottom);
pack();
}
}