好的,你不小心遇到了一個怪癖。雖然佈局看起來很簡單,但 也讓我花了一些時間來解決這個問題。 (順便說一句,水平拉伸按鈕不是一個最佳的UI設計。)
原因是管理者的方式。如果佈局中不包含 連續的至少兩個組件,則無法跨組件。 您的佈局僅包含一列,因此,設置widthx
> 1確實 不起作用。有兩種方法可以解決這個問題:我們可以在佈局中放置一些 虛擬元件,或者我們可以在 之前和之後放置一些空間來減小它們的寬度。
我提供了三種解決方案:a)具有虛擬標籤的GridBagLayout,b)具有插入的GridBagLayout 和c)帶有間隙的MigLayout。
溶液1
package com.zetcode;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class FillingCards extends JPanel {
private JTextArea question;
private JTextArea answer;
private JButton button;
private JLabel label;
public FillingCards() {
setupGUI();
}
private void setupGUI() {
question = new JTextArea(12, 22);
answer = new JTextArea(12, 22);
button = new JButton("Next");
label = new JLabel("Answer");
question.setBorder(
BorderFactory.createEtchedBorder()
);
answer.setBorder(
BorderFactory.createEtchedBorder()
);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.insets = new Insets(5, 10, 5, 10);
gbc.gridx = 1;
gbc.gridy = 0;
add(question, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weighty = 0;
gbc.weightx = 0;
add(label, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.weighty = 1;
add(answer, gbc);
gbc.gridwidth = 3;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(button, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
gbc.weighty = 0;
gbc.weightx = 0;
gbc.gridx = 0;
gbc.gridy = 3;
add(new JLabel(" "), gbc);
gbc.gridx = 2;
gbc.gridy = 3;
add(new JLabel(" "), gbc);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Quiz cards");
FillingCards card = new FillingCards();
frame.add(BorderLayout.CENTER, card);
frame.pack();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
兩個標籤被放置在所述第一和第三單元,下一步按鈕的下方。 這不是一個乾淨的解決方案,主要是爲了演示問題而創建的。
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
gbc.weighty = 0;
gbc.weightx = 0;
gbc.gridx = 0;
gbc.gridy = 3;
add(new JLabel(" "), gbc);
gbc.gridx = 2;
gbc.gridy = 3;
add(new JLabel(" "), gbc);
創建了兩個虛擬標籤。這些標籤的寬度決定了左側和右側間隙的寬度 。
溶液2
package com.zetcode;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class FillingCards2 extends JPanel {
private JTextArea question;
private JTextArea answer;
private JButton button;
private JLabel label;
public FillingCards2() {
setupGUI();
}
private void setupGUI() {
question = new JTextArea(12, 22);
answer = new JTextArea(12, 22);
button = new JButton("Next");
label = new JLabel("Answer");
question.setBorder(
BorderFactory.createEtchedBorder()
);
answer.setBorder(
BorderFactory.createEtchedBorder()
);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.insets = new Insets(5, 15, 5, 15);
gbc.gridx = 1;
gbc.gridy = 0;
add(question, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weighty = 0;
gbc.weightx = 0;
add(label, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.weighty = 1;
add(answer, gbc);
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.gridwidth = 3;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(button, gbc);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Quiz cards");
FillingCards2 card = new FillingCards2();
frame.add(BorderLayout.CENTER, card);
frame.pack();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
在這個例子中,我們使用insets
屬性來創建所需 佈局。標籤和文本區域的左側和右側插入符號更寬,即那些按鈕。
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.gridwidth = 3;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(button, gbc);
左右插槽從15px更改爲5px。這樣我們實現了 該按鈕比其他組件寬。
溶液3
第三溶液用MigLayout
管理器創建。我們需要較少的 代碼來創建佈局。
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import net.miginfocom.swing.MigLayout;
public class MigLayoutFillingCards extends JFrame {
public MigLayoutFillingCards() {
initUI();
setTitle("Quiz cards");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap"));
JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
area1.setBorder(
BorderFactory.createEtchedBorder()
);
area2.setBorder(
BorderFactory.createEtchedBorder()
);
JLabel label = new JLabel("Answer");
JButton btn = new JButton("Next");
add(area1, "w 250, h 150, grow, push, gap 15 15 0 0");
add(label, "gap 15 15 0 0");
add(area2, "w 250, h 150, grow, push, gap 15 15 0 0");
add(btn, "growx, pushx");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutFillingCards ex = new MigLayoutFillingCards();
ex.setVisible(true);
}
});
}
}
所需的佈局用gap
約束,這增加了額外的空間 到組件的側面產生。
GBC是基於列 – mKorbel
對於[示例](http://stackoverflow.com/q/14755487/230513)。 – trashgod