2016-05-03 14 views
0

私のコードに問題があり、JComboBoxが表示され、好きなときに消えるようです。私はJComboBoxが動作しているときのインスタンスを持っていて、単にプログラムを終了して再起動すると、再び動作を停止します。何が起こっている?JComboBoxは表示されません。私は間違って何をしていますか?

class FutoshikiGUI extends JFrame { 
    JPanel main = new JPanel(); 
    public FutoshikiGUI() { 
     super("Futoshiki"); 

     //setup standard GUI parameters 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
     setResizable(false); 
     setSize(600,600); 
     add(main); 

     //initialise other GUI components 
     initPanel(); 
    } 

    private void initPanel() { 
     //prepare main panel 
     main.setLayout(new GridBagLayout()); 
     main.setBackground(Color.decode("#FFFFFF")); 

     //create and prepare title 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     JLabel title = new JLabel("Futoshiki"); 
     title.setFont(new Font("Tahoma", Font.PLAIN, 20)); 
     main.add(title, gbc); 

     //create and prepare combobox 
     gbc.gridy = 1; 
     String[] diff = {"Easy", "Med", "Hard"}; 
     JComboBox difficulty = new JComboBox(diff); 
     difficulty.setSelectedIndex(0); 
     main.add(difficulty); 
    } 
+3

はしないでください、あなたの 'JFrame'目に見えるすべてのコンポーネントが追加されました前に。 – Berger

+0

ああ、私はそれが間違いだったのは分かっていたが、それは簡単だろうとは知らなかった。ありがとう! –

+1

また、 'initPanel()'の後に 'add(main)'を呼んでください:) – Berger

答えて

3

あなたはすべてのコンポーネントがJFrameに追加された一度だけsetVisible(true)を呼び出す必要があります。

また、JPanelも準備ができていることを確認するために、initPanel()add(main)を呼び出す:

public FutoshikiGUI() { 
    super("Futoshiki"); 

    //setup standard GUI parameters 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 


    //initialise other GUI components 
    initPanel(); 

    add(main); 

    setResizable(false); 
    setSize(600,600); 

    setVisible(true); 
} 
関連する問題