[英]Component Spacing Using GridLayout
I am having a problem using the Java GridLayout
.我在使用 Java
GridLayout
时遇到问题。 To begin with, I am using the grid layout to simply put two JLabel
components with their corresponding JTextField
controls but the problem I find is that I have too much space between the label and the textField and I don't know how to correct it.首先,我使用网格布局简单地将两个
JLabel
组件及其对应的JTextField
控件放置在一起,但我发现的问题是 label 和 textField 之间的空间太大,我不知道如何纠正它。
JPanel panel2 = new JPanel ();
estableceBorde(panel2, "Info Jinete y Caballo ");
GridLayout panel21;
panel21 = new GridLayout(2,2);
panel21.setHgap(0);
panel21.setVgap(10);
panel2.setLayout(panel21);
panel2.add(jinete);
panel2.add(jinetet);
panel2.add(caballo);
panel2.add(caballot);
This is the capture with that spacing between the JLabel and JTextField that I mean:这是我的意思是 JLabel 和 JTextField 之间的间距的捕获:
The problem with GridLayout is that all grid boxes must be exactly the same size, which is great if that is the layout that you desire, such as with a chessboard, but this is not so great if you need a more flexible grid. GridLayout 的问题在于所有网格框的大小必须完全相同,如果这是您想要的布局,例如棋盘,这很好,但如果您需要更灵活的网格,这不是很好。 Myself, I would use a different layout, such as a GridBagLayout which gives greater flexibility to your positioning and gaps.
我自己,我会使用不同的布局,例如 GridBagLayout,它可以为您的定位和间隙提供更大的灵活性。 For example:
例如:
import java.awt.*;
import javax.swing.*;
public class Foo001 {
public static void main(String[] args) {
JPanel panel2 = new JPanel (new GridBagLayout());
GridBagConstraints gbc = createGbc(0, 0);
panel2.add(new JLabel("Jinete"), createGbc(0, 0));
panel2.add(new JTextField(15), createGbc(1, 0));
panel2.add(new JLabel("Caballo"), createGbc(0, 1));
panel2.add(new JTextField(15), createGbc(1, 1));
panel2.setBorder(BorderFactory.createTitledBorder("Info Jinete y Caballo"));
// Show the JPanel for demonstration purposes
JOptionPane.showMessageDialog(null, panel2);
}
private static GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
int gap = 3;
gbc.insets = new Insets(gap, gap + 2 * gap * x, gap, gap);
return gbc;
}
}
The key to this code, and the difficulty in using GridBagLayout, is to correctly set the GridBagConstraints, something that I often do using a utility method, here createGbc(...)
.这段代码的关键,以及使用 GridBagLayout 的困难,是正确设置 GridBagConstraints,这是我经常使用实用方法执行的操作,这里
createGbc(...)
。 The gridx and gridy settings are obvious, as this is where the components are placed relative to each other. gridx 和 gridy 设置很明显,因为这是组件相对于彼此放置的位置。 The insets are the gaps around the components, and I've given the JTextFields a larger left inset to separate them a little more from the respective JLabels: `
插图是组件周围的间隙,我给了 JTextFields 一个更大的左侧插图,以便将它们与各自的 JLabels 分开一点:`
new Insets(gap, gap + 2 * gap * x, gap, gap);
The left pad, gap + 2 * gap * x
is gap
size (here 3) when x is 0 (when at the first column), and is gap
x 3 when x is 1.左边的焊盘,
gap + 2 * gap * x
是当 x 为 0(在第一列时)时的gap
大小(此处为 3),当 x 为 1 时是gap
x 3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.