2016-04-13 19 views
-2

私はJavaのスイングに取り組んでおり、UIレイアウトがついています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

EXPECTED DISPLAY

enter image description here

答えて

3

行を交換:

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

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

あなたはこのケースでJScrollPanelあるjpOPanelに追加しているコンポーネントのGridBagConstraintsを設定する必要があります。

+0

お世話になりました。しかし、私の内側のパネルでは、チェックボックスと他の2つのコンポーネントが同じ行に入っています。私が望むのは、チェックボックスは最初の行に、2行目にはラベルとチェックボックスを入れることです。両方ともインナーパネルの境界線に合わせる必要があります。参照のために元の投稿の画像を参照してください – pankti

+0

@panktiまず、 'jInnerPanel'のレイアウトマネージャを' GridBagLayout'に設定する必要があります。これは 'jInnerPanel.setLayout(new GridBagLayout())'で行うことができます。次に、 'JComponent'にはそれぞれ' GridBagConstraint'を持たせることをお勧めします。 'jCheckBox1'、' jlLabel7'、 'jComboBox4'の右側の' gridx'と 'gridy'値でこれを行うと、あなたは期待しているものを得るはずです。 –

+0

あなたの時間と応答のためにトンMr.Lekiliに感謝します。 – pankti