如何保持 2 个 JPanel 彼此垂直对齐?
How to keep 2 JPanels aligned vertically to each other?
好的,我对 Java swings 还很陌生,正在尝试学习它的技巧,但正在努力解决框架中 2 个 JPanel 的对齐问题。如果您查看下面的屏幕截图,我的列名称 JPanel 对齐方式及其下面的 JPanel 对齐方式没有正确对齐。
下面是我的代码。
JPanel columns = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1.0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.VERTICAL;
text = "<html><b>Action</b></html>";
JLabel label_action = new JLabel(text);
columns.add(label_action, gbc);
columns.setLayout(new GridBagLayout());
JPanel values= new JPanel(new GridBagLayout());
values.setBorder(BorderFactory.createRaisedBevelBorder());
gbc.weighty = 1.0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.VERTICAL;
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
model.addElement("click");
model.addElement("Input Keys");
final JComboBox<String> comboBox1 = new JComboBox<String>(model);
AutoCompleteDecorator.decorate(comboBox1);
comboBox1.setPreferredSize(new Dimension(200, 22));
values.add(comboBox1, gbc);
values.setLayout(new GridBagLayout());
甚至不必费心与 GridBagLayout
合作。它是一个设计不佳的布局管理器,不符合当今的要求。我推荐
MigLayout
或 GroupLayout
.
看看使用 MigLayout
创建布局是多么容易:
package com.zetcode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class MigLayoutAlignmentEx extends JFrame {
public MigLayoutAlignmentEx() {
initUI();
}
private void initUI() {
setLayout(new MigLayout());
JLabel lbl1 = new JLabel("Action");
JLabel lbl2 = new JLabel("Choose Selector");
JLabel lbl3 = new JLabel("Element Properties");
JLabel lbl4 = new JLabel("Values");
JLabel lbl5 = new JLabel("Element Name");
JLabel lbl6 = new JLabel("Comments");
JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();
JTextField field1 = new JTextField(15);
JTextField field2 = new JTextField(15);
JTextField field3 = new JTextField(15);
JTextField field4 = new JTextField(15);
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
add(lbl5);
add(lbl6, "wrap");
add(combo1, "w 150lp");
add(combo2, "w 150lp");
add(field1);
add(field2);
add(field3);
add(field4, "wrap");
pack();
setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MigLayoutAlignmentEx ex = new MigLayoutAlignmentEx();
ex.setVisible(true);
});
}
}
您的 UI 非常容易创建;您只需将组件放入单元格中即可。但是,与预先创建网格的 MigLayout
不同,
GridBagLayout
您必须手动创建每个单元格,这很困难且容易出错。
好的,我对 Java swings 还很陌生,正在尝试学习它的技巧,但正在努力解决框架中 2 个 JPanel 的对齐问题。如果您查看下面的屏幕截图,我的列名称 JPanel 对齐方式及其下面的 JPanel 对齐方式没有正确对齐。
下面是我的代码。
JPanel columns = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1.0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.VERTICAL;
text = "<html><b>Action</b></html>";
JLabel label_action = new JLabel(text);
columns.add(label_action, gbc);
columns.setLayout(new GridBagLayout());
JPanel values= new JPanel(new GridBagLayout());
values.setBorder(BorderFactory.createRaisedBevelBorder());
gbc.weighty = 1.0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.VERTICAL;
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
model.addElement("click");
model.addElement("Input Keys");
final JComboBox<String> comboBox1 = new JComboBox<String>(model);
AutoCompleteDecorator.decorate(comboBox1);
comboBox1.setPreferredSize(new Dimension(200, 22));
values.add(comboBox1, gbc);
values.setLayout(new GridBagLayout());
甚至不必费心与 GridBagLayout
合作。它是一个设计不佳的布局管理器,不符合当今的要求。我推荐
MigLayout
或 GroupLayout
.
看看使用 MigLayout
创建布局是多么容易:
package com.zetcode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class MigLayoutAlignmentEx extends JFrame {
public MigLayoutAlignmentEx() {
initUI();
}
private void initUI() {
setLayout(new MigLayout());
JLabel lbl1 = new JLabel("Action");
JLabel lbl2 = new JLabel("Choose Selector");
JLabel lbl3 = new JLabel("Element Properties");
JLabel lbl4 = new JLabel("Values");
JLabel lbl5 = new JLabel("Element Name");
JLabel lbl6 = new JLabel("Comments");
JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();
JTextField field1 = new JTextField(15);
JTextField field2 = new JTextField(15);
JTextField field3 = new JTextField(15);
JTextField field4 = new JTextField(15);
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
add(lbl5);
add(lbl6, "wrap");
add(combo1, "w 150lp");
add(combo2, "w 150lp");
add(field1);
add(field2);
add(field3);
add(field4, "wrap");
pack();
setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MigLayoutAlignmentEx ex = new MigLayoutAlignmentEx();
ex.setVisible(true);
});
}
}
您的 UI 非常容易创建;您只需将组件放入单元格中即可。但是,与预先创建网格的 MigLayout
不同,
GridBagLayout
您必须手动创建每个单元格,这很困难且容易出错。