我想使用GridBagLayout
布局如图所示的组件
我已经尝试了几个约束,但它永远不会得到预期的结果,所以我开始怀疑它是否真的只有GridBagLayout
。困难在于C1,C2和C3组件
C1和C2是JComponent
,其中包含其他组件,如JPanel
。我已经设定了他们的最小和首选尺寸。 C3是JButton
C1不应占用额外的空间,所以我将它的权重x设置为0,将网格宽度设置为1(同时尝试使用2,因为它跨越C2和C3)。
C2占用了所有额外的空间,我将它的权重x设置为1,网格宽度设置为3
GUI不可调整大小
我已多次使用此LayoutManager
但仍未掌握它,谢谢你的帮助。
答案 0 :(得分:3)
我只会谈论GridBagLayout
,即使这可能完全适用于MigLayout
(MigLayout还有其他参数用于填充列和行数,调整大小,ei),以及/或TableLayout(???)
GridBagLayout
只需填充第一行中所有所需数量的列(仅限),然后创建矩阵,您可以定义GBC weightx, weighty, gridx, gridy
和/或Anchor
太
谈论
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class GbcLayout {
private JFrame frame = new JFrame("GbcLayoutGbcLayout");
private JPanel panel = new JPanel();
private JLabel hidelLabel;
private JLabel firstLabel;
private JTextField firstText;
public GbcLayout() {
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
for (int k = 0; k < 50; k++) {
hidelLabel = new JLabel(" ");
hidelLabel.setOpaque(true);
hidelLabel.setBackground(Color.orange);
hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.weighty = 0.5;
gbc.gridx = k;
gbc.gridy = 0;
panel.add(hidelLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 0;
gbc.gridwidth = 8;
gbc.gridy = k + 1;
panel.add(firstLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstText = new JTextField("Testing TextField");
firstText.setFont(new Font("Serif", Font.BOLD, 20));
firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 9;
gbc.gridwidth = k + 8;
gbc.gridy = k + 1;
panel.add(firstText, gbc);
}
for (int k = 0; k < 5; k++) {
firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 20 + k;
gbc.gridwidth = 8;
gbc.gridy = k + 1;
panel.add(firstLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstText = new JTextField("Testing TextField");
firstText.setFont(new Font("Serif", Font.BOLD, 20));
firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 29 + k;
gbc.gridwidth = 21 - k;
gbc.gridy = k + 1;
panel.add(firstText, gbc);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GbcLayout gbcl = new GbcLayout();
}
});
}
}
答案 1 :(得分:1)
我担心这是不可能的。 GridBagLayout
无法弄清楚C1的开始和C3的开始之间的适当距离。