我想只使用GridBagLayout
.
我已经尝试了几个约束,但它从来没有达到预期的结果,所以我开始怀疑它是否真的可能只使用GridBagLayout
. 困难在于 C1、C2 和 C3 组件。
C1 和 C2JComponent
将在内部保存其他组件,例如JPanel
. 我已经设置了它们的最小和首选尺寸。C3 是一个JButton
。
C1 不应占用额外空间,因此我将其 weightx 设置为 0,将 gridwidth 设置为 1(也尝试使用 2,因为它跨越 C2 和 C3)。
C2 占用所有额外空间,我将其 weightx 设置为 1 并将 gridwidth 设置为 3。GUI
不可调整大小。
这个我用了LayoutManager
好几次了,还是没掌握,谢谢大家的帮助。
问问题
195 次
2 回答
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();
}
});
}
}
于 2012-11-30T07:26:52.897 回答
1
恐怕这是不可能的。GridBagLayout
无法弄清楚 C1 的起点和 C3 的起点之间的正确距离是多少。
于 2012-11-30T07:12:45.397 回答