這個問題幾乎總是由不正確設置GridBagConstraints.weightx和GridBagConstraints.weighty特性造成的。它們的默認值是0,它告訴組件在中心以近似首選大小的大小聚集在一起。在所有組件上給它們兩個並觀察發生了什麼。
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.anchor=GridBagConstraints.FIRST_LINE_START;
gbc1.insets = new Insets(5,5,5,5);
// ***** add: *****
gbc1.weightx = 1.0;
gbc1.weighty = 1.0;
這告訴佈局在x和y方向都展開,如果網格袋單元有擴展空間的話。
如果這沒有幫助,那麼您將需要創建併發布Minimal, Complete, and Verifiable example。
一兩件事:通常,如果你讓所有的組件力爭達到他們的首選大小最好最賞心悅目的佈局得以實現,這是最好的避免調用大部分部件setSize(...)
或setPreferredSize(...)
進行,而是簡單地在頂部調用pack()
級別窗口顯示之前。
編輯
在評論你的狀態:
我已經嘗試過這一點。它確實將佈局對齊到右上角,但它也分散了面板上的組件。我正在尋找保持相同的羣集。我希望所有的標籤和radiobtns是由(像圖)
收盤價,那麼你想要做的是使用和窩至少2個 JPanels,第一個使用的GridBagLayout並保持你的GUI組件如上所示,第二個持有第一個GridBagLayout - 使用JPanel。這第二個JPanel可以使用BoxLayout或FlowLayout(FlowLayout.LEFT),或者其他可能的佈局或佈局組合。
例如:
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridBagLayoutEg extends JPanel {
private static final Insets INSETS = new Insets(5, 5, 5, 5);
private static final int PREF_W = 800;
private static final int PREF_H = 600;
public GridBagLayoutEg() {
JPanel innerPanel = new JPanel(new GridBagLayout());
innerPanel.add(new JLabel("Sequence name: abcdc"), createGbc(0, 0));
GridBagConstraints gbc = createGbc(1, 0);
gbc.gridwidth = 3;
innerPanel.add(new JLabel(), gbc);
innerPanel.add(new JLabel("Block making alighment tool:", SwingConstants.LEFT), createGbc(0, 1));
innerPanel.add(new JRadioButton("Mafft"), createGbc(1, 1));
innerPanel.add(new JRadioButton("Muscle"), createGbc(2, 1));
innerPanel.add(new JRadioButton("ClusteIO"), createGbc(3, 1));
innerPanel.add(new JLabel("Select Codon Table:", SwingConstants.LEFT), createGbc(0, 2));
innerPanel.add(new JRadioButton("Standard"), createGbc(1, 2));
innerPanel.add(new JRadioButton("Custom"), createGbc(2, 2));
innerPanel.add(new JLabel(), createGbc(3, 2));
innerPanel.add(new JLabel("Strictness:", SwingConstants.LEFT), createGbc(0, 3));
innerPanel.add(new JTextField(2), createGbc(1, 3));
innerPanel.add(new JLabel("Degeneracy:", SwingConstants.LEFT), createGbc(0, 4));
innerPanel.add(new JTextField(2), createGbc(1, 4));
setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
add(innerPanel);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.insets = INSETS;
gbc.fill = GridBagConstraints.HORIZONTAL;
return gbc;
}
private static void createAndShowGui() {
JFrame frame = new JFrame("GridBagLayoutEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GridBagLayoutEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
'默認GridBagLayout',將圍繞容器中的所有組件。您可以使用'weightx'和'weighty'來改變給定元件如何填充容器內的剩餘空間,並使用位於其他元件右側/下方的簡單「空」填充元件將給出所需的影響。沒有任何文本的'JLabel'可以在這裏很好地工作 – MadProgrammer