Java Swing UI组件布局问题

时间:2016-04-13 06:49:36

标签: java swing jpanel layout-manager gridbaglayout

我正在研究java swing,我遇到了UI布局

我目前的输出如下。标签6之后的文本区域正在缩小,下一个面板将在它旁边。 我想要的是面板应该在下一行,文本区域应该与组合框的大小相同。

enter image description here

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;
gbl.setConstraints(jpSearch, gbc);
jpOPanel.add(jpSearch);

JLabel jlLabel1 = new JLabel("Label 1 :");
gbc.weightx = 0.0D;
gbc.weighty = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.fill = 2;
gbc.gridwidth = -1;
gbl.setConstraints(jlLabel1, gbc);
jpOPanel.add(jlLabel1);

JComboBox jComboBox1 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbl.setConstraints(jComboBox1, gbc);
jComboBox1.setVisible(true);
jpOPanel.add(jComboBox1);

JLabel jlLabel2 = new JLabel("Label 2 :");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbl.setConstraints(jlLabel2, gbc);
jpOPanel.add(jlLabel2);

JComboBox jComboBox2 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbl.setConstraints(jComboBox2, gbc);
jComboBox2.setVisible(true);
jpOPanel.add(jComboBox2);

JLabel jlLabel3 = new JLabel("Label 3 :");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbl.setConstraints(jlLabel3, gbc);
jpOPanel.add(jlLabel3);

JComboBox jComboBox3 = new JComboBox();
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbl.setConstraints(jComboBox3, gbc);
jpOPanel.add(jComboBox3);

JLabel jlLabel4 = new JLabel("Label 4 :");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbl.setConstraints(jlLabel4, gbc);
jpOPanel.add(jlLabel4);

JTextField jTextField1 = new JTextField();
jTextField1.setEditable(false);
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbl.setConstraints(jTextField1, gbc);
jpOPanel.add(jTextField1);

JLabel jlLabel5 = new JLabel("Label 5 :");
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.gridwidth = -1;
gbl.setConstraints(jlLabel5, gbc);
jpOPanel.add(jlLabel5);

JTextField jTextField2 = new JTextField();
jTextField2.setEditable(false);
gbc.weightx = 1.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 0;
gbl.setConstraints(jTextField2, gbc);
jpOPanel.add(jTextField2);

JLabel jlLabel6 = new JLabel("Label 6 :");
jlLabel6.setVisible(true);
gbc.weightx = 0.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridwidth = 1;
gbl.setConstraints(jlLabel6, gbc);
jpOPanel.add(jlLabel6);

JTextArea jTextArea1 = new JTextArea(3, 35);
jTextArea1.setVisible(true);
jTextArea1.setEditable(false); 
jTextArea1.setLineWrap(true);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

JScrollPane jspane = new JScrollPane(jTextArea1, v, h);
gbc.weightx = 2;
gbc.weighty = 0.0D;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 0;
gbc.gridheight = 2;
gbl.setConstraints(jTextArea1, gbc);
jpOPanel.add(jspane);
jspane.setVisible(true);

JPanel jInnerPanel = new JPanel();
jInnerPanel.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
gbc.weightx = 0.0D;
gbc.weighty = 0.0D;
gbc.insets = new Insets(5, 0, 5, 5);
gbc.fill = 1;
gbc.gridwidth = 0;
gbl.setConstraints(jInnerPanel, gbc);

JCheckBox jCheckBox1 = new JCheckBox("Check here");
jCheckBox1.setPreferredSize(new Dimension(140, 20));
jCheckBox1.setMinimumSize(new Dimension(140, 20));
jCheckBox1.setMaximumSize(new Dimension(140, 20));
gbc.ipady = 0;
gbc.weightx = 0.5D;
gbc.weighty = 0.0D;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(jCheckBox1, gbc);
jInnerPanel.add(jCheckBox1);


JLabel jlLabel7 = new JLabel("Label 7 : ");
jlLabel7.setPreferredSize(new Dimension(140, 20));
jlLabel7.setMinimumSize(new Dimension(140, 20));
jlLabel7.setMaximumSize(new Dimension(140, 20));
gbc.ipady = 0;
gbc.weightx = 0.5D;
gbc.weighty = 0.0D;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx = 0;
gbc.gridy = 6;

gbl.setConstraints(jlLabel7, gbc);
jInnerPanel.add(jlLabel7);

JComboBox jComboBox4 = new JComboBox();
jComboBox4.setEnabled(false);
jComboBox4.setPreferredSize(new Dimension(225, 20));
jComboBox4.setMinimumSize(new Dimension(225, 20));
jComboBox4.setMaximumSize(new Dimension(225, 20));
gbc.gridx = 1;
gbc.gridy = 6;
gbc.anchor = GridBagConstraints.LAST_LINE_END;
gbl.setConstraints(jComboBox4, gbc);
jInnerPanel.add(jComboBox4);

jpOPanel.add(jInnerPanel);

frame.add(jpOPanel);

当前显示 enter image description here

预期展示 enter image description here

1 个答案:

答案 0 :(得分:3)

替换行:

 gbl.setConstraints(jTextArea1, gbc);
 jpOPanel.add(jspane);

jspane.setViewportView(jTextArea1);
jpOPanel.add(jspane, gbc);

在这种情况下,您需要将GridBagConstraints的{​​{1}}设置为jpOPanel