2017-05-17 10 views
0

私のプログラムに問題がある場合、コードの最初の実行は完全に実行されますが、新しいオブジェクトを作成してから、正しいタイトルで正しいサイズで表示されていますが、コンポーネントは表示されません。新しいオブジェクトが作成されたときにJFrameがコンポーネントを表示しない

スタティック変数がないことを確認しました。すべての必要な変数がコンストラクタで初期化されています。ウィンドウを表示

クラス:

public class DifficultySelect implements ActionListener   
{ 
    private JPanel getInput; 
    private JFrame startFrame; 
    private JButton easy; 
    private JButton medium; 
    private JButton hard; 
    private JButton custom; 
    private JLabel labelRow; 
    private JLabel labelColumn; 
    private JLabel labelMines; 
    private JTextArea textRow; 
    private JTextArea textColumn; 
    private JTextArea textMines; 
    public ArrayList<Integer> getArray; 

    public DifficultySelect() 
    { 
     getInput = new JPanel(); 
     startFrame = new JFrame("Select Difficulty:"); 
     easy = new JButton(); 
     medium = new JButton(); 
     hard = new JButton(); 
     custom = new JButton(); 
     labelRow = new JLabel(); 
     labelColumn = new JLabel(); 
     labelMines = new JLabel(); 
     textRow = new JTextArea(5, 20); 
     textColumn = new JTextArea(5, 20); 
     textMines = new JTextArea(5, 20); 
     getArray = new ArrayList<Integer>(); 
    } 

    public void setDisplay() 
    { 
     getInput.setLayout(new BoxLayout(getInput, BoxLayout.PAGE_AXIS));  
     getInput.setVisible(true); 

     Dimension buttonSize = new Dimension(300,40); 
     easy.setText("Easy: 5x5 - 4 Mines"); 
     easy.setMaximumSize(buttonSize); 
     easy.addActionListener(this); 
     medium.setText("Medium: 10x10 - 20 Mines"); 
     medium.setMaximumSize(buttonSize); 
     medium.addActionListener(this); 
     hard.setText("Hard: 15 x 15 - 50 Mines"); 
     hard.setMaximumSize(buttonSize); 
     hard.addActionListener(this); 
     custom.setText("Custom: Enter Rows/Columns/Mines then Click"); 
     custom.setMaximumSize(buttonSize); 
     custom.addActionListener(this); 

     labelRow.setText("Enter Row Size: ");  
     labelColumn.setText("Enter Column Size: ");  
     labelMines.setText("Enter Amount of Mines:"); 

     textRow.setAlignmentX(0); 
     textColumn.setAlignmentX(0); 
     textMines.setAlignmentX(0); 

     getInput.add(easy); 
     getInput.add(medium); 
     getInput.add(hard); 
     getInput.add(custom); 
     getInput.add(labelRow); 
     getInput.add(textRow); 
     getInput.add(labelColumn); 
     getInput.add(textColumn); 
     getInput.add(labelMines); 
     getInput.add(textMines); 

     startFrame.add(getInput); 
     startFrame.setSize(310, 250); 
     startFrame.setResizable(false); 
     startFrame.setVisible(true); 
     startFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

コードの次のブロックがプログラム

に後で呼び出されたときに、私は、メインから呼び出されたときに動作するGUIクラスを呼び出していますが、動作しない方法を示しています
private DifficultySelect d; 

    public StartGame() 
    { 

     d = new DifficultySelect(); 
     d.setDisplay(); 
    } 

私はこの作業を行うためにさまざまな方法を試してきましたが、現在はコンストラクタで呼び出しがありますので、正しいですが白い画面が空白になるStartGame newGame = new StartGame();というオブジェクトを呼び出していますサイズ、および持っています正しいJFrame名。これは、いくつかの側面が正しく作成されていることを意味します。

+0

[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –

+0

JPanelやJFrameを 'repaint()'しようとしましたか? –

+0

私は再描画を試みましたが、成功しませんでした – Pengoid

答えて

1

してください、あなたのGUIを起動するためにそれを試してみてください。

public class StartGame { 

    public static void main(String[] args) { 
    DifficultySelect d = new DifficultySelect(); 
     d.setDisplay(); 

    } 
} 

に見えます: enter image description here

をもう一度したいときは、コードを使用することができ、別の場所でこのフォームを表示:

 if (d == null) { 
      d = new DifficultySelect(); 
      d.setVisible(true); 
     } else { 
      d.setVisible(true); 
     } 

新しいインスタンスを作成する場合は、このコードを次の場所に配置します。

DifficultySelect new_d = new DifficultySelect(); 
    new_d.setDisplay(); 
+0

あなたが私に本当に何を言っているのか分かりません、それは私のプログラムが最初の実行でどのように見えるかです。そのフレームを作り直して問題が始まる方法を思い出さなければならない頃までは、そうではありません。 – Pengoid

+0

それはどういう意味ですか? –

+0

それが問題を解決しました。ありがとうございます。私は新しいなしでそれを作成していた – Pengoid

関連する問題