Swing JButton,Java编程

时间:2015-01-06 11:09:11

标签: java swing

程序假设运行"猜数字"游戏 在正确猜出数字之后,可以选择重新开始

按" playAgainBtn"使程序卡住。 另一个问题是在猜测" guessText"不能.selectAll

欢迎任何见解

TNX。

public class GuessTheNumberGame extends JFrame
{
    private int randomNumber;
    private boolean correctGuess;
    private JLabel startMsg;
    private JLabel enterGuessJLabel;
    private JButton playAgainBtn;
    private JLabel msgToPlayer;
    private JTextField guessText;
    private int previousGuess; // previous guessed number hot/cold
    private int numOfGuess;
    private Container container;
    public GuessTheNumberGame()
    {
        container = getContentPane();
        startMsg = new JLabel();
        enterGuessJLabel = new JLabel();
        guessText = new JTextField(10);
        guessText.addActionListener(
                new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                        int  playerGuess = Integer.parseInt(event.getActionCommand());
                        if ( playerGuess == randomNumber )
                        {
                            msgToPlayer.setText("Great ! u guessed in " + numOfGuess + "Guesses");
                            correctGuess = true;
                        }
                        else 
                        {
                            wrongGuess(playerGuess);
                        }
                    }

                }
                );
        msgToPlayer = new JLabel("");
        playAgainBtn = new JButton("Play Again");
        ButtonHandler buttonHandler = new ButtonHandler();
        playAgainBtn.addActionListener(buttonHandler);
        setLayout(new FlowLayout());
        add(startMsg);
        add(enterGuessJLabel);
        add(guessText);
        add(msgToPlayer);
        add(playAgainBtn);

    }

    protected class ButtonHandler implements ActionListener 
    {

        @Override
        public void actionPerformed(ActionEvent event)
        {
            startGame();
        }

    }

    public void startGame()
    {
        previousGuess = 1000;
        numOfGuess = 1;
        correctGuess = false;
        Random rand = new Random();
        randomNumber = rand.nextInt(1000) + 1;
        startMsg.setText( "I have a number between 1 and 1000. can you Guess my Number?" );
        playAgainBtn.setVisible( false );

        while ( !correctGuess)
        {
            enterGuessJLabel.setText( "Please enter your " + numOfGuess + " guess: " );
        }

        playAgainBtn.setVisible(true);
    }

    private void wrongGuess(int playerGuess)
    {
        numOfGuess++;
        if ( Math.abs(playerGuess - randomNumber) > previousGuess )
                container.setBackground( Color.GREEN);
        else 
            container.setBackground( Color.RED);
        previousGuess = Math.abs(playerGuess - randomNumber);

        if (playerGuess > randomNumber)
            msgToPlayer.setText("Too High");
        else 
            msgToPlayer.setText( "Too Low" );
    }

}

2 个答案:

答案 0 :(得分:0)

问题是你的startGame方法在单击按钮时调用,其中有一个无限循环。

while ( !correctGuess)
    {
        enterGuessJLabel.setText( "Please enter your " + numOfGuess + " guess: " );
    }

将永远不会更改correctGuess变量,因此您的程序将无限地执行setText命令。

Swing(以及所有GUI框架)的工作方式是,您必须始终响应按钮单击等事件,然后返回控件。

答案 1 :(得分:0)

启动应用程序时,startGame()在线程上运行,这是正常的。按playAgainBtn时,它会调用 AWT Dispatch Thread 上的startGame(),从而占用此线程,禁止AWT处理事件。试试这个以释放AWT Dispatch Thread:

protected class ButtonHandler implements ActionListener 
{

    @Override
    public void actionPerformed(ActionEvent event)
    {
        new Thread() {
            @Override
            public final void run() {
                startGame();
            }
        }.start();
    }
}