[英]GridBagLayout difficulties
我想使用GridBagLayout
布局如圖所示的組件。
我已經嘗試了幾個約束,但它永遠不會得到預期的結果,所以我想知道它是否真的可能只有GridBagLayout
。 困難在於C1,C2和C3組件。
C1和C2是JComponent
,它將像JPanel
一樣容納其他組件。 我已經設定了他們的最小和首選尺寸。 C3是JButton
。
C1不應該占用額外的空間,所以我將它的權重x設置為0,將網格寬度設置為1(同時嘗試使用2,因為它跨越C2和C3)。
C2占用了所有額外的空間,我將它的weightx設置為1,將gridwidth設置為3。
GUI不可調整大小。
我已經多次使用這個LayoutManager
但仍然沒有掌握它,謝謝你的幫助。
我只會談論GridBagLayout
,即使這可能完全適用於MigLayout
(MigLayout還有其他參數用於填充Columns&Rows數量,調整大小,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();
}
});
}
}
我擔心這是不可能的。 GridBagLayout
無法弄清楚C1的開始和C3的開始之間的適當距離。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.