2011-09-15 12 views
7

sto ottenendo questo errore su questo codice:Exception in thread "main" java.awt.AWTError: BoxLayout non può essere condivisa

super("Trace Masker"); 
    setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)); 

    label1 = new JLabel("Source directory:"); 
    label2 = new JLabel("Target directory:"); 
    label3 = new JLabel("Defect number:"); 
    label4 = new JLabel("Slice tokens:"); 
    label4.setToolTipText("Seperate multiple tokens with comma"); 

    txtSourceDirectory = new JTextField(30); 
    txtTargetDirectory = new JTextField(30); 
    txtDefectNumber = new JTextField(30); 
    txtSliceTokens = new JTextField(30); 

    btnBrowseSourceDirectory = new JButton("..."); 
    btnBrowseTargetDirectory = new JButton("..."); 
    btnStart = new JButton("Start"); 
    btnCancel = new JButton("Cancel"); 

    pnlLabels = new JPanel(new BoxLayout(pnlLabels, BoxLayout.PAGE_AXIS)); 
    pnlText = new JPanel(new BoxLayout(pnlText, BoxLayout.PAGE_AXIS)); 
    pnlBrowseButtons = new JPanel(new BoxLayout(pnlBrowseButtons, BoxLayout.PAGE_AXIS)); 
    pnlTop = new JPanel(new BoxLayout(pnlTop, BoxLayout.LINE_AXIS)); 
    pnlActionButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 

    pnlLabels.add(label1); 
    pnlLabels.add(label2); 
    pnlLabels.add(label3); 
    pnlLabels.add(label4); 

    pnlText.add(txtSourceDirectory); 
    pnlText.add(txtTargetDirectory); 
    pnlText.add(txtDefectNumber); 
    pnlText.add(txtSliceTokens); 

    pnlBrowseButtons.add(btnBrowseSourceDirectory); 
    pnlBrowseButtons.add(btnBrowseTargetDirectory); 

    pnlTop.add(pnlLabels); 
    pnlTop.add(pnlText); 
    pnlTop.add(pnlBrowseButtons); 

    pnlActionButtons.add(btnStart); 
    pnlActionButtons.add(btnCancel); 

    add(pnlTop); 
    add(pnlActionButtons); 

L'errore è su questa linea:

pnlLabels.add(label1); 

Giusto per verificare se questo si riferiva specificamente a pnlLabels, ho commentato tutte le sue righe. L'errore si verifica quel momento in poi:

pnlText.add(txtSourceDirectory); 

ho già controllato le altre 2 domande qui su questo e fissato la dichiarazione setLayout per il JFrame: Question1 Question2

risposta

12

Il tuo problema deriva dal seguente riga (e tutte le altre linee alla ricerca dello stesso):

pnlLabels = new JPanel(new BoxLayout(pnlLabels, BoxLayout.PAGE_AXIS)); 

Quando new BoxLayout(...) si chiama, pnlLabels è ancora null poiché non è ancora stato assegnato. Il modo corretto per farlo è in due fasi:

pnlLabels = new JPanel(); 
pnlLabels.setLayout(new BoxLayout(pnlLabels, BoxLayout.PAGE_AXIS); 

Il problema dovrebbe scomparire (a patto di farlo per tutte le altre linee di codice simile a quello).

+2

1+ battere il mio di 20 secondi. :) –

+0

Grazie, questo lo ha risolto. – Yoav

Problemi correlati