2012-11-22 65 views
37

我在JPanel中添加複選框FlowLayout複選框被水平添加。垂直添加控件而不是水平使用流佈局

我想在面板上垂直添加複選框。什麼是可能的解決方案?

+1

的FlowLayout是做什麼暗示,流動組件從左到右,直到它有沒有空間然後繼續下一行,使用不同的佈局,你可以做你需要的。 – AbstractChaos

+0

我應該使用什麼佈局 – adesh

+2

我會建議一個[BoxLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html) – AbstractChaos

回答

36

我希望你正在努力實現是這樣的。爲此請使用Box佈局。

package com.kcing.kailas.sample.client; 

import javax.swing.BoxLayout; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.WindowConstants; 

public class Testing extends JFrame { 

private static final long serialVersionUID = 1L; 
private JPanel jContentPane = null; 

/** 
* This is the default constructor 
*/ 
public Testing() { 
    super(); 
    initialize(); 
} 

/** 
* This method initializes this 
* 
* @return void 
*/ 
private void initialize() { 
    this.setSize(300, 200); 
    this.setContentPane(getJContentPane()); 
    this.setTitle("JFrame"); 
} 

/** 
* This method initializes jContentPane 
* 
* @return javax.swing.JPanel 
*/ 
private JPanel getJContentPane() { 
    if (jContentPane == null) { 
     jContentPane = new JPanel(); 
     jContentPane.setLayout(null); 

     JPanel panel = new JPanel(); 

     panel.setBounds(61, 11, 81, 140); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     jContentPane.add(panel); 

     JCheckBox c1 = new JCheckBox("Check1"); 
     panel.add(c1); 
     c1 = new JCheckBox("Check2"); 
     panel.add(c1); 
     c1 = new JCheckBox("Check3"); 
     panel.add(c1); 
     c1 = new JCheckBox("Check4"); 
     panel.add(c1); 


    } 
    return jContentPane; 
} 
public static void main(String[] args) throws Exception { 
    Testing frame = new Testing(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
} 
} 
+8

'UIManager.setLookAndFeel(「com.sun.java.swing.plaf.windows.WindowsLookAndFeel」);'唉..看起來可怕的OS X和* nix,雖然幸運的是它會完全失敗在任一系統上。請參閱['UIManager.getSystemLookAndFeelClassName()'](http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName%28%29)。 –

+1

感謝您的評論安德魯湯普森。我正在刪除這個。 –

+4

...和基本方法(使用BoxLayout)不同於@AbstractChaos在之前的答案中。除了更糟糕:內容面板中的空佈局?手動上漿/定位面板? nononono,不要。 updateComponentTreeUI應該在這裏實現什麼? – kleopatra

9

正如我在評論中所述,我會使用一個盒子佈局。

JPanel panel = new JPanel(); 
panel.setLayout(new BoxLayout()); 

JButton button = new JButton("Button1"); 
button.setAlignmentX(Component.CENTER_ALIGNMENT); 
panel.add(button); 

button = new JButton("Button2"); 
button.setAlignmentX(Component.CENTER_ALIGNMENT); 
panel.add(button); 

button = new JButton("Button3"); 
button.setAlignmentX(Component.CENTER_ALIGNMENT); 
panel.add(button); 

add(panel); 
+0

這一個不是編譯 –

34

我用了一個BoxLayout,並設置其第二個參數BoxLayout.Y_AXIS和它的工作對我來說:

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
+6

+1提到'BoxLayout.Y_AXIS'。 – ArtOfWarfare

+0

是的,這項工作也適合我。 –

+0

如何將單個元素對齊到右側並使其他所有元素都居中? –

2
JPanel testPanel = new JPanel(); 
testPanel.setLayout(new BoxLayout(testPanel, BoxLayout.Y_AXIS)); 
/*add variables here and add them to testPanel 
     e,g`enter code here` 
     testPanel.add(nameLabel); 
     testPanel.add(textName); 
*/ 
testPanel.setVisible(true); 
+2

有什麼解釋? – Opal

+0

歡迎來到Stack Overflow!請考慮在BoxLayout中添加一個鏈接,以便改進它,並提供一個簡短的解釋。 – juhist

相關問題