Java Swing UI 对齐问题
Java Swing UI alignment issue
我有一个要求 UI 应该显示如下图。
我尝试使用下面的代码实现同样的效果。
package com.samples;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class MyFrame extends JFrame {
public MyFrame() {
}
public static void main(String[] args) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("My Sample Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.setVisible(true);
JPanel jpOPanel = new JPanel();
jpOPanel.setBorder(new CompoundBorder(new TitledBorder("Outer Panel"),
new EmptyBorder(0, 2, 2, 2)));
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpOPanel.setLayout(gbl);
JPanel jpSearch = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton jbSearch = new JButton("Search");
jpSearch.add(jbSearch);
gbc.weightx = 0.0D;
gbc.weighty = 0.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.fill = 1;
gbc.gridwidth = 0;
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(jpSearch, gbc);
jpOPanel.add(jpSearch);
JLabel jLabel1 = new JLabel("Input 1:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(jLabel1, gbc);
jpOPanel.add(jLabel1);
JComboBox jComboBox1 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 1;
gbl.setConstraints(jComboBox1, gbc);
jComboBox1.setVisible(true);
jpOPanel.add(jComboBox1);
JLabel jLabel2 = new JLabel("Input 2:");
gbc.weightx = 0.0D;
gbc.weighty = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.fill = 2;
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 2;
gbl.setConstraints(jLabel2, gbc);
jpOPanel.add(jLabel2);
JComboBox jComboBox2 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 2;
gbl.setConstraints(jComboBox2, gbc);
jComboBox2.setVisible(true);
jpOPanel.add(jComboBox2);
JLabel jLabel3 = new JLabel("Input 3:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 3;
gbl.setConstraints(jLabel3, gbc);
jpOPanel.add(jLabel3);
JComboBox jComboBox3 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 3;
gbl.setConstraints(jComboBox3, gbc);
jpOPanel.setVisible(true);
jpOPanel.add(jComboBox3);
JPanel jpPanel1 = new JPanel();
jpPanel1.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
GridBagConstraints gbc3 = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 9;
gbl.setConstraints(jpPanel1, gbc);
JLabel jLabel4 = new JLabel("Input 4:");
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 0;
gbc3.gridy = 0;
gbc3.gridwidth = -1;
jpPanel1.add(jLabel4, gbc3);
JTextField jTextField1 = new JTextField();
jTextField1.setEditable(false);
jTextField1.setPreferredSize(new Dimension(100, 20));
jTextField1.setMinimumSize(new Dimension(100, 20));
jTextField1.setMaximumSize(new Dimension(100, 20));
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 1;
gbc3.gridy = 0;
gbc3.gridwidth = 0;
jpPanel1.add(jTextField1, gbc3);
JLabel jLabel5 = new JLabel("Input 5:");
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 2;
gbc3.gridy = 0;
gbc3.gridwidth = -1;
jpPanel1.add(jLabel5, gbc3);
JTextField jTextField2 = new JTextField();
jTextField2.setEditable(false);
jTextField2.setPreferredSize(new Dimension(100, 20));
jTextField2.setMinimumSize(new Dimension(100, 20));
jTextField2.setMaximumSize(new Dimension(100, 20));
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 3;
gbc3.gridy = 0;
gbc3.gridwidth = 0;
jpPanel1.add(jTextField2, gbc3);
jpOPanel.add(jpPanel1);
JLabel jLabel6 = new JLabel("Input 6:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 15;
gbl.setConstraints(jLabel6, gbc);
jpOPanel.add(jLabel6);
JComboBox jComboBox4 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 15;
gbl.setConstraints(jComboBox4, gbc);
jpOPanel.add(jComboBox4);
JLabel jLabel7 = new JLabel("Input 7:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 16;
gbl.setConstraints(jLabel7, gbc);
jpOPanel.add(jLabel7);
JTextField jTextField3 = new JTextField();
jTextField3.setEditable(false);
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 16;
gbl.setConstraints(jTextField3, gbc);
jpOPanel.add(jTextField3);
JPanel jpPanel3 = new JPanel();
jpPanel3.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
GridBagConstraints gbc2 = new GridBagConstraints();
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 24;
gbl.setConstraints(jpPanel3, gbc);
JCheckBox jCheckBox1 = new JCheckBox("Check Me");
jCheckBox1.setPreferredSize(new Dimension(140, 20));
jCheckBox1.setMinimumSize(new Dimension(140, 20));
jCheckBox1.setMaximumSize(new Dimension(140, 20));
gbc2.ipady = 0;
gbc2.weightx = 0.5D;
gbc2.weighty = 0.0D;
gbc2.gridwidth = 1;
gbc2.anchor = GridBagConstraints.LINE_START;
gbc2.insets = new Insets(0, 0, 0, 0);
gbc2.gridx = 0;
gbc2.gridy = 0;
jpPanel3.add(jCheckBox1, gbc2);
JLabel jLabel8 = new JLabel("Input 8: ");
jLabel8.setPreferredSize(new Dimension(140, 20));
jLabel8.setMinimumSize(new Dimension(140, 20));
jLabel8.setMaximumSize(new Dimension(140, 20));
gbc2.ipady = 0;
gbc2.weightx = 0.5D;
gbc2.weighty = 0.0D;
gbc2.anchor = GridBagConstraints.LINE_START;
gbc2.insets = new Insets(0, 0, 0, 0);
gbc2.gridx = 0;
gbc2.gridy = 6;
jpPanel3.add(jLabel8, gbc2);
JComboBox jComboBox5 = new JComboBox();
jComboBox5.setEnabled(false);
jComboBox5.setPreferredSize(new Dimension(230, 20));
jComboBox5.setMinimumSize(new Dimension(230, 20));
jComboBox5.setMaximumSize(new Dimension(230, 20));
gbc2.gridx = 3;
gbc2.gridy = 6;
jpPanel3.add(jComboBox5, gbc2);
jpOPanel.add(jpPanel3);
frame.add(jpOPanel);
}
};
EventQueue.invokeLater(runner);
}
}
但是组件显示如下图所示。请帮忙改正。
您可以尝试在最左边添加一个Box.createHorizontalGlue()
,但您必须确保它具有最大的gridbagconstraints.weightx;
例如:
gbc.weightx = 1;//needs to be more than 0 so it works.
gbc.gridx = 4;//needs to be the largest gridx in the panel + 1
jpPanel3.add(Box.createHorizontalGlue(), gbc2);
注意:请尽量减少您的代码片段,使其更易于其他人阅读。
通常在使用 GridBagLayout
时,您必须使用权重为 1 的面板来填写您想要空的 space。首先,设置 gbc.fill = GridBagConstraints.BOTH;
.
然后,添加空面板并使用权重填充 space,如下图所示。上方和下方的面板应占据所有位置,因此 weighty = 1
;并且还跨越两列,所以 gridwidth = 2
。中间的面板占据剩余的高度。右侧的面板应占用剩余的宽度,因此 weightx = 1
。这样您的面板将始终只占用剩余的 space.
我有一个要求 UI 应该显示如下图。
我尝试使用下面的代码实现同样的效果。
package com.samples;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class MyFrame extends JFrame {
public MyFrame() {
}
public static void main(String[] args) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("My Sample Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.setVisible(true);
JPanel jpOPanel = new JPanel();
jpOPanel.setBorder(new CompoundBorder(new TitledBorder("Outer Panel"),
new EmptyBorder(0, 2, 2, 2)));
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpOPanel.setLayout(gbl);
JPanel jpSearch = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton jbSearch = new JButton("Search");
jpSearch.add(jbSearch);
gbc.weightx = 0.0D;
gbc.weighty = 0.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.fill = 1;
gbc.gridwidth = 0;
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(jpSearch, gbc);
jpOPanel.add(jpSearch);
JLabel jLabel1 = new JLabel("Input 1:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(jLabel1, gbc);
jpOPanel.add(jLabel1);
JComboBox jComboBox1 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 1;
gbl.setConstraints(jComboBox1, gbc);
jComboBox1.setVisible(true);
jpOPanel.add(jComboBox1);
JLabel jLabel2 = new JLabel("Input 2:");
gbc.weightx = 0.0D;
gbc.weighty = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.fill = 2;
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 2;
gbl.setConstraints(jLabel2, gbc);
jpOPanel.add(jLabel2);
JComboBox jComboBox2 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 2;
gbl.setConstraints(jComboBox2, gbc);
jComboBox2.setVisible(true);
jpOPanel.add(jComboBox2);
JLabel jLabel3 = new JLabel("Input 3:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 3;
gbl.setConstraints(jLabel3, gbc);
jpOPanel.add(jLabel3);
JComboBox jComboBox3 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 3;
gbl.setConstraints(jComboBox3, gbc);
jpOPanel.setVisible(true);
jpOPanel.add(jComboBox3);
JPanel jpPanel1 = new JPanel();
jpPanel1.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
GridBagConstraints gbc3 = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 9;
gbl.setConstraints(jpPanel1, gbc);
JLabel jLabel4 = new JLabel("Input 4:");
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 0;
gbc3.gridy = 0;
gbc3.gridwidth = -1;
jpPanel1.add(jLabel4, gbc3);
JTextField jTextField1 = new JTextField();
jTextField1.setEditable(false);
jTextField1.setPreferredSize(new Dimension(100, 20));
jTextField1.setMinimumSize(new Dimension(100, 20));
jTextField1.setMaximumSize(new Dimension(100, 20));
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 1;
gbc3.gridy = 0;
gbc3.gridwidth = 0;
jpPanel1.add(jTextField1, gbc3);
JLabel jLabel5 = new JLabel("Input 5:");
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 2;
gbc3.gridy = 0;
gbc3.gridwidth = -1;
jpPanel1.add(jLabel5, gbc3);
JTextField jTextField2 = new JTextField();
jTextField2.setEditable(false);
jTextField2.setPreferredSize(new Dimension(100, 20));
jTextField2.setMinimumSize(new Dimension(100, 20));
jTextField2.setMaximumSize(new Dimension(100, 20));
gbc3.insets = new Insets(0, 0, 0, 0);
gbc3.gridx = 3;
gbc3.gridy = 0;
gbc3.gridwidth = 0;
jpPanel1.add(jTextField2, gbc3);
jpOPanel.add(jpPanel1);
JLabel jLabel6 = new JLabel("Input 6:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 15;
gbl.setConstraints(jLabel6, gbc);
jpOPanel.add(jLabel6);
JComboBox jComboBox4 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 15;
gbl.setConstraints(jComboBox4, gbc);
jpOPanel.add(jComboBox4);
JLabel jLabel7 = new JLabel("Input 7:");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbc.gridx = 0;
gbc.gridy = 16;
gbl.setConstraints(jLabel7, gbc);
jpOPanel.add(jLabel7);
JTextField jTextField3 = new JTextField();
jTextField3.setEditable(false);
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbc.gridx = 1;
gbc.gridy = 16;
gbl.setConstraints(jTextField3, gbc);
jpOPanel.add(jTextField3);
JPanel jpPanel3 = new JPanel();
jpPanel3.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
GridBagConstraints gbc2 = new GridBagConstraints();
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 24;
gbl.setConstraints(jpPanel3, gbc);
JCheckBox jCheckBox1 = new JCheckBox("Check Me");
jCheckBox1.setPreferredSize(new Dimension(140, 20));
jCheckBox1.setMinimumSize(new Dimension(140, 20));
jCheckBox1.setMaximumSize(new Dimension(140, 20));
gbc2.ipady = 0;
gbc2.weightx = 0.5D;
gbc2.weighty = 0.0D;
gbc2.gridwidth = 1;
gbc2.anchor = GridBagConstraints.LINE_START;
gbc2.insets = new Insets(0, 0, 0, 0);
gbc2.gridx = 0;
gbc2.gridy = 0;
jpPanel3.add(jCheckBox1, gbc2);
JLabel jLabel8 = new JLabel("Input 8: ");
jLabel8.setPreferredSize(new Dimension(140, 20));
jLabel8.setMinimumSize(new Dimension(140, 20));
jLabel8.setMaximumSize(new Dimension(140, 20));
gbc2.ipady = 0;
gbc2.weightx = 0.5D;
gbc2.weighty = 0.0D;
gbc2.anchor = GridBagConstraints.LINE_START;
gbc2.insets = new Insets(0, 0, 0, 0);
gbc2.gridx = 0;
gbc2.gridy = 6;
jpPanel3.add(jLabel8, gbc2);
JComboBox jComboBox5 = new JComboBox();
jComboBox5.setEnabled(false);
jComboBox5.setPreferredSize(new Dimension(230, 20));
jComboBox5.setMinimumSize(new Dimension(230, 20));
jComboBox5.setMaximumSize(new Dimension(230, 20));
gbc2.gridx = 3;
gbc2.gridy = 6;
jpPanel3.add(jComboBox5, gbc2);
jpOPanel.add(jpPanel3);
frame.add(jpOPanel);
}
};
EventQueue.invokeLater(runner);
}
}
但是组件显示如下图所示。请帮忙改正。
您可以尝试在最左边添加一个Box.createHorizontalGlue()
,但您必须确保它具有最大的gridbagconstraints.weightx;
例如:
gbc.weightx = 1;//needs to be more than 0 so it works.
gbc.gridx = 4;//needs to be the largest gridx in the panel + 1
jpPanel3.add(Box.createHorizontalGlue(), gbc2);
注意:请尽量减少您的代码片段,使其更易于其他人阅读。
通常在使用 GridBagLayout
时,您必须使用权重为 1 的面板来填写您想要空的 space。首先,设置 gbc.fill = GridBagConstraints.BOTH;
.
然后,添加空面板并使用权重填充 space,如下图所示。上方和下方的面板应占据所有位置,因此 weighty = 1
;并且还跨越两列,所以 gridwidth = 2
。中间的面板占据剩余的高度。右侧的面板应占用剩余的宽度,因此 weightx = 1
。这样您的面板将始终只占用剩余的 space.