2016-05-18 22 views
1

我正在做Java Swing上的任務,並且正在製作GUI猜測遊戲程序。我的問題是當我按下「猜測」按鈕什麼也沒有發生,我不能通過點擊X來關閉它,我必須用Eclipse來終止它。我做錯了什麼? GuessHandler是否正確實現了ActionListener,還是我在那裏做錯了什麼?無法獲得Java Swing GUI猜測遊戲工作

public class GuessingGameGui extends JFrame 
{ 
    public static final int WIDTH = 600; 
    public static final int HEIGHT = 400; 
    private JTextField theText; 
    private JLabel message; 
    private JPanel p1; 
    private JPanel p2; 
    private int guess; 
    private int numberOfTries = 0; 

    public GuessingGameGui() 
    { 
     super(); 
     setSize(WIDTH, HEIGHT); 
     //set the window title to "Guessing Game" 
     setTitle("Guessing Game"); 
     Container c = getContentPane(); 
     c.setLayout(new BorderLayout()); 
     c.setBackground(Color.WHITE); 

     p1 = new JPanel(); 
     p2 = new JPanel(); 

     p1.setBackground(Color.WHITE); 
     p2.setBackground(Color.BLUE); 

     //"add a JButton called "Guess" 
     JButton guessButton = new JButton("Guess"); 
     GuessHandler ghandler = new GuessHandler(); 
     guessButton.addActionListener(ghandler); 
     p1.add(guessButton); 

     //The north panel will have a JLabel with the text "Guess a number between 1 and 10?" 
     JLabel label1 = new JLabel("Guess a number between 1 and 10?"); 
     c.add(label1, BorderLayout.NORTH); 

     //The south panel will have a JLabel for displaying if the user guessed correctly or not 
     message = new JLabel(""); 
     p2.add(message, BorderLayout.SOUTH); 
     c.add(p2, BorderLayout.SOUTH); 


     JPanel textPanel = new JPanel(); 
     textPanel.setBackground(Color.LIGHT_GRAY); 

     //In the center panel, add a JTextField where the user can enter a number to guess 
     theText = new JTextField(10); 
     theText.setBackground(Color.WHITE); 
     textPanel.add(theText); 
     textPanel.add(p1); 
     c.add(textPanel, BorderLayout.CENTER); 
    } 

    public static void main(String[] args) 
    { 
     GuessingGameGui guessGame = new GuessingGameGui(); 
     guessGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guessGame.setSize(WIDTH, HEIGHT); 
     guessGame.setVisible(true); 
    } 

    class GuessHandler implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      int numberToGuess = (int) (Math.random() * 10 + 1); 
      Scanner input = new Scanner (System.in); 
      boolean win = false; 
      while (win == false){ 

       guess = input.nextInt(); 
       numberOfTries++; 
       if (guess < 1 || guess > 10) 
       { 
        //Make the south panel background color RED if they entered an invalid number (not between 1 and 10) 
        p2.setBackground(Color.RED); 
       } 
       else if (guess == numberToGuess) 
       { 
        win = true; 
        //Make the south panel background color YELLOW if they guessed right 
        p2.setBackground(Color.YELLOW); 
        //and display "YOU GOT IT (n attempts)", where n is the number of attempts the user guessed 
        message.setText("YOU GOT IT (" +numberOfTries + "attempts)"); 
       } 
       else 
       { 
        //Make the south panel background color GREY if they guessed wrong 
        p2.setBackground(Color.GRAY); 
        //display "Sorry try again" 
        message.setText("Sorry try again"); 
       } 
      } 
     } 
    } 
} 
+0

由於你的程序正在鎖定,我認爲你的actionhandler有一個無限循環,所以win永遠不會設置爲true 。你也從控制檯獲得輸入,而不是從你想要的JTextfield –

回答

3

有你的程序中的幾個問題,導致您的問題:

  1. 你不應該有一個while循環在你的方法actionPerformed,否則你的Swing應用程序將凍結。
  2. 您不需要Scanner,因爲猜測值是從文本字段中提取的值,因此guess = input.nextInt()應該是guess = Integer.parseInt(theText.getText())。事實上,否則您的應用程序將凍結,直到在standard input stream中提供integer,這不是您期望的。
+0

這就是它。這兩點都會導致你的應用程序中斷:while循環將無限期地繼續,而無法響應新的輸入。掃描儀將永遠阻止永遠等待控制檯輸入。 –

0

它可以是有用的,總是添加到您的框架:

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
frame.addWindowListener(new WindowAdapter() { 
    @Override 
    public void windowClosing(final WindowEvent e) { 
     handleExitRequest(); 
    } 
}); 

,然後創建一個方法handleExitRequest()做實際的接近(也許只是調用System.exit(0);) 您也可以從「退出」菜單項的actionPerformed()中調用該方法(如果有)。這允許你清理退出前應該完成的任何事情(比如說提交或回滾任何數據庫操作等)