gpt4 book ai didi

java - GridBagLayout 困难

转载 作者:行者123 更新时间:2023-11-29 03:39:59 26 4
gpt4 key购买 nike

我想只使用 GridBagLayout 来布局组件,如图所示。
我已经尝试了几个约束,但它永远不会以预期的结果结束,所以我想知道仅使用 GridBagLayout 是否真的可行。难点在于C1、C2、C3组件。
C1 和 C2 是 JComponent,它将包含其他组件,如 JPanel。我已经设置了它们的最小尺寸和首选尺寸。 C3 是一个JButton
C1 不应该占用额外的空间,所以我将其 weightx 设置为 0,gridwidth 设置为 1(也尝试使用 2,因为它跨越 C2 和 C3)。
C2 占用所有额外空间,我将其 weightx 设置为 1,将 gridwidth 设置为 3。
GUI 不可调整大小。
这个LayoutManager我已经用了好几次了,但还是没有掌握,谢谢你的一点帮助。

GridBagLayout

最佳答案

  • 我只会谈论 GridBagLayout,即使这可能正是 MigLayout 的工作(MigLayout 有额外的参数用于填充列数和行数,调整大小, e.i.) 和/或 TableLayout(???)

  • GridBagLayout 只需要在第一行(仅)填充所有需要的列数,然后创建矩阵,您可以定义任何 GBC weightx, weighty, gridx, gridy 和/或也使用 Anchor

  • 例子谈论

enter image description here

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();
}
});
}
}

关于java - GridBagLayout 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13640283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com