如何获取 GridBagLayout 来安排列宽?
How to get a GridBagLayout to arrange column widths?
这是一个非常简单的布局(包含代码),其中不均匀的列大小可以改善填充。
有没有办法使用单个 GridBagLayout
使组件很好地组合在一起?
我认为对前两个组件使用 3 的宽度,然后对接下来的两行使用 5 和 1 的宽度会影响对齐,但我无法让它保持齐平。我当前的解决方案(不包括在内)是创建一个包含红色和绿色面板的子面板。
import javax.swing.*;
import java.awt.*;
public class GridBagWrong{
public static void main(String[] args){
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//create five panels to layout.
JPanel a = new JPanel();
a.setPreferredSize( new Dimension(300, 30) );
a.setBorder( BorderFactory.createLineBorder(Color.BLUE) );
JPanel b = new JPanel();
b.setPreferredSize( new Dimension(300, 30) );
b.setBorder( BorderFactory.createLineBorder(Color.BLUE) );
JPanel c = new JPanel();
c.setPreferredSize( new Dimension(500, 30) );
c.setBorder( BorderFactory.createLineBorder(Color.GREEN) );
JPanel d = new JPanel();
d.setPreferredSize( new Dimension(500, 30) );
d.setBorder( BorderFactory.createLineBorder(Color.GREEN) );
JPanel e = new JPanel();
e.setPreferredSize( new Dimension(100, 60) );
e.setBorder( BorderFactory.createLineBorder(Color.RED) );
//place them in a gridbaglayout.
gbc.gridwidth = 3;
content.add(a, gbc);
gbc.gridx = 3;
content.add(b, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 5;
content.add(c, gbc);
gbc.gridy = 2;
content.add(d, gbc);
gbc.gridy = 1;
gbc.gridx = 5;
gbc.gridheight = 2;
gbc.gridwidth = 1;
content.add(e, gbc);
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane( content );
frame.pack();
frame.setVisible(true);
}
}
出于某种原因 GridBagLayout
正在对其计算添加加法 space,但我不确定为什么。起初我以为是LineBorder
,但是当我修改你的代码去除边框时,还是不行...
不知道右边多出来的间距是哪里来的(应该是很明显,但是我看不出来)
Sooo,这是我“翻转 table”和“非常愤怒”而不是“破解它”的时刻之一
由于第一行和第二行之间似乎存在脱节,出于某种原因,我将第一行的两个面板合并为一个面板并去掉了所有 gridwidth
s(更多或更少)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//create five panels to layout.
JPanel a = new LayoutPane();
a.setForeground(Color.BLUE);
a.setPreferredSize(new Dimension(300, 30));
JPanel b = new LayoutPane();
b.setPreferredSize(new Dimension(300, 30));
b.setForeground(Color.BLUE);
JPanel c = new LayoutPane();
c.setPreferredSize(new Dimension(500, 30));
c.setForeground(Color.GREEN);
JPanel d = new LayoutPane();
d.setPreferredSize(new Dimension(500, 30));
d.setForeground(Color.GREEN);
JPanel e = new LayoutPane();
e.setPreferredSize(new Dimension(100, 60));
e.setForeground(Color.RED);
JPanel topPane = new JPanel(new GridBagLayout());
//place them in a gridbaglayout.
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPane.add(a, gbc);
gbc.gridx++;
topPane.add(b, gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(topPane, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
add(c, gbc);
gbc.gridy = 2;
add(d, gbc);
gbc = new GridBagConstraints();
gbc.gridy = 1;
gbc.gridx++;
gbc.gridheight = 2;
gbc.anchor = GridBagConstraints.EAST;
add(e, gbc);
}
}
public class LayoutPane extends JPanel {
public LayoutPane() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getForeground());
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
String text = getWidth() + "x" + getHeight();
FontMetrics fm = g2d.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(text, x, y);
g2d.dispose();
}
}
}
根据我上面的评论,您可以使用:
//JPanel content = new JPanel(new GridBagLayout());
GridBagLayout gbl = new GridBagLayout();
int[] columns = new int[6];
Arrays.fill(columns, 100);
gbl.columnWidths = columns;
JPanel content = new JPanel(gbl);
以上代码规定 6 列中的每一列的最小宽度为 100。
如果使用 gridwidth = 1
将组件添加到列,则该组件的首选大小将用作列的宽度。
所以现在当您指定 gridwidth = 3
时它是有意义的,因为布局可以使用添加到列的组件的宽度或最小宽度来确定组件的实际大小。
这是一个非常简单的布局(包含代码),其中不均匀的列大小可以改善填充。
有没有办法使用单个 GridBagLayout
使组件很好地组合在一起?
我认为对前两个组件使用 3 的宽度,然后对接下来的两行使用 5 和 1 的宽度会影响对齐,但我无法让它保持齐平。我当前的解决方案(不包括在内)是创建一个包含红色和绿色面板的子面板。
import javax.swing.*;
import java.awt.*;
public class GridBagWrong{
public static void main(String[] args){
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//create five panels to layout.
JPanel a = new JPanel();
a.setPreferredSize( new Dimension(300, 30) );
a.setBorder( BorderFactory.createLineBorder(Color.BLUE) );
JPanel b = new JPanel();
b.setPreferredSize( new Dimension(300, 30) );
b.setBorder( BorderFactory.createLineBorder(Color.BLUE) );
JPanel c = new JPanel();
c.setPreferredSize( new Dimension(500, 30) );
c.setBorder( BorderFactory.createLineBorder(Color.GREEN) );
JPanel d = new JPanel();
d.setPreferredSize( new Dimension(500, 30) );
d.setBorder( BorderFactory.createLineBorder(Color.GREEN) );
JPanel e = new JPanel();
e.setPreferredSize( new Dimension(100, 60) );
e.setBorder( BorderFactory.createLineBorder(Color.RED) );
//place them in a gridbaglayout.
gbc.gridwidth = 3;
content.add(a, gbc);
gbc.gridx = 3;
content.add(b, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 5;
content.add(c, gbc);
gbc.gridy = 2;
content.add(d, gbc);
gbc.gridy = 1;
gbc.gridx = 5;
gbc.gridheight = 2;
gbc.gridwidth = 1;
content.add(e, gbc);
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane( content );
frame.pack();
frame.setVisible(true);
}
}
出于某种原因 GridBagLayout
正在对其计算添加加法 space,但我不确定为什么。起初我以为是LineBorder
,但是当我修改你的代码去除边框时,还是不行...
不知道右边多出来的间距是哪里来的(应该是很明显,但是我看不出来)
Sooo,这是我“翻转 table”和“非常愤怒”而不是“破解它”的时刻之一
由于第一行和第二行之间似乎存在脱节,出于某种原因,我将第一行的两个面板合并为一个面板并去掉了所有 gridwidth
s(更多或更少)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//create five panels to layout.
JPanel a = new LayoutPane();
a.setForeground(Color.BLUE);
a.setPreferredSize(new Dimension(300, 30));
JPanel b = new LayoutPane();
b.setPreferredSize(new Dimension(300, 30));
b.setForeground(Color.BLUE);
JPanel c = new LayoutPane();
c.setPreferredSize(new Dimension(500, 30));
c.setForeground(Color.GREEN);
JPanel d = new LayoutPane();
d.setPreferredSize(new Dimension(500, 30));
d.setForeground(Color.GREEN);
JPanel e = new LayoutPane();
e.setPreferredSize(new Dimension(100, 60));
e.setForeground(Color.RED);
JPanel topPane = new JPanel(new GridBagLayout());
//place them in a gridbaglayout.
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPane.add(a, gbc);
gbc.gridx++;
topPane.add(b, gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(topPane, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
add(c, gbc);
gbc.gridy = 2;
add(d, gbc);
gbc = new GridBagConstraints();
gbc.gridy = 1;
gbc.gridx++;
gbc.gridheight = 2;
gbc.anchor = GridBagConstraints.EAST;
add(e, gbc);
}
}
public class LayoutPane extends JPanel {
public LayoutPane() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getForeground());
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
String text = getWidth() + "x" + getHeight();
FontMetrics fm = g2d.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(text, x, y);
g2d.dispose();
}
}
}
根据我上面的评论,您可以使用:
//JPanel content = new JPanel(new GridBagLayout());
GridBagLayout gbl = new GridBagLayout();
int[] columns = new int[6];
Arrays.fill(columns, 100);
gbl.columnWidths = columns;
JPanel content = new JPanel(gbl);
以上代码规定 6 列中的每一列的最小宽度为 100。
如果使用 gridwidth = 1
将组件添加到列,则该组件的首选大小将用作列的宽度。
所以现在当您指定 gridwidth = 3
时它是有意义的,因为布局可以使用添加到列的组件的宽度或最小宽度来确定组件的实际大小。