2012-11-22 139 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

相关问题