2012-11-22 14 views
37

Aggiungo le caselle di controllo su JPanel in FlowLayout le caselle di controllo vengono aggiunte orizzontalmente.aggiungi controlli verticalmente anziché orizzontalmente utilizzando il layout del flusso

Desidero aggiungere caselle di controllo in verticale sul pannello. Qual è la possibile soluzione?

+1

FlowLayout sta facendo quello che suggerisce, componenti che scorre da sinistra a destra fino a che non ha spazio e poi passa alla riga successiva, con layout diversi puoi fare ciò di cui hai bisogno. – AbstractChaos

+0

quale layout dovrei usare per questo – adesh

+2

Vorrei suggerire un [BoxLayout] (http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html) – AbstractChaos

risposta

36

Spero che quello che si sta cercando di realizzare è così. Per questo per favore usa il layout della scatola.

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 ");' Ughh .. che apparirebbe orrido su OS X & * nix, anche se fortunatamente fallirebbe completamente su entrambi i sistemi. Vedi invece [UIManager.getSystemLookAndFeelClassName() '] (http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName%28%29). –

+1

Grazie per il commento Andrew Thompson. Sto rimuovendo questo. –

+4

... e l'approccio di base (usa BoxLayout) differisce dalla risposta precedente di @AbstractChaos in quanto ..? Tranne che peggio: layout nullo in contentpane? dimensionamento manuale/posizionamento del pannello? nononono, no. E qual è l'updateComponentTreeUI che dovrebbe raggiungere qui? – kleopatra

9

Come ho dichiarato nel commento, avrei usato un layout di scatola per questo.

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

questo non si sta compilando –

34

ho usato un BoxLayout e impostare il secondo parametro come BoxLayout.Y_AXIS e ha funzionato per me:

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

+1 per aver menzionato 'BoxLayout.Y_AXIS'. – ArtOfWarfare

+0

sì, questo lavoro anche per me. –

+0

Come allineare un singolo elemento a destra e centrare tutto il resto? –

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

Qualche spiegazione? – Opal

+0

Benvenuti in Stack Overflow! Ti preghiamo di considerare di aggiungere un link a BoxLayout alla tua risposta per migliorarlo e una breve spiegazione. – juhist

Problemi correlati