温暖,热,& GUI猜测游戏的冷逻辑部分? - Java

时间:2017-04-19 20:43:43

标签: java user-interface logic

所以我正在使用一个简单的Java GUI猜测游戏,用户必须猜测一个随机数。如果用户的猜测接近该数字,则窗口变为红色(热),橙色(暖),蓝色(冷),但是逻辑部分出现问题。我这就是我所要做的。此外,我将背景设置为特定颜色,但颜色仅在窗口第一次出现时快速显示。那是为什么?

import javax.swing.*;
import java.awt.event.*;

import java.awt.*;
import java.util.Random;
import java.awt.Color;

public class GuessingGame extends JFrame {
private JButton newGameButton;
private JButton enterButton;
private JButton exitButton;
private JTextField guessBox;
private JLabel initialTextLabel;
private JLabel enterLabel;
private JLabel userMessageLabel;
private int userGuess;
private int randomNumber;
private int counter = 0;
private int lastGuess = 0;
private int difference = 0;

public GuessingGame() {
    super("Guessing Game");
    newGameButton = new JButton("New Game");
    exitButton = new JButton("Exit Game");
    enterButton = new JButton("Enter");
    guessBox = new JTextField(4);
    initialTextLabel = new JLabel("I have a number between 1 and 1000. Can you guess it?");
    enterLabel = new JLabel("Please enter your first guess.");
    userMessageLabel = new JLabel("");
    randomNumber = new Random().nextInt(1000) + 1;
    setBackground(Color.RED);

    setLayout(new FlowLayout());
    add(initialTextLabel);
    add(enterLabel);
    add(guessBox);
    add(newGameButton);
    add(enterButton);
    add(exitButton);
    add(userMessageLabel);

    setSize(500, 125);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

    });

    newGameButtonHandler newghandler = new newGameButtonHandler();
    newGameButton.addActionListener(newghandler);

    exitButtonHandler exithandler = new exitButtonHandler();
    exitButton.addActionListener(exithandler);

    enterButtonHandler enterhandler = new enterButtonHandler();
    enterButton.addActionListener(enterhandler);    
}
class newGameButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        setBackground(Color.ORANGE);
        userMessageLabel.setText("");
        randomNumber = new Random().nextInt(1000) + 1;
    }
}
class exitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}
class enterButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        userGuess = Integer.parseInt(guessBox.getText());
        compareGuess(userGuess, randomNumber);
    }
}
public void compareGuess(int userGuess, int randomNumber) {
    counter++;
    if(userGuess == randomNumber) {
        userMessageLabel.setText("You got it! It took you: " + counter + "guess(es)");
            setBackground(Color.GREEN);
    }
    else if (userGuess > randomNumber) {

        userMessageLabel.setText("Too high");
    }
    else if(userGuess < randomNumber) {
        userMessageLabel.setText("Too low");
    }
     if(counter > 1) {
        if ((randomNumber - userGuess) > (randomNumber - lastGuess))
            {
                setBackground(Color.RED);
            }
        else if ((randomNumber - userGuess) < (randomNumber - lastGuess))
            {
                setBackground(Color.BLUE);
            }
        else
            {
                setBackground(Color.GRAY);
            }
    }
    lastGuess = userGuess;
}

public static void main(String[] args)  {
    GuessingGame myGuessingGame = new GuessingGame(); 
    myGuessingGame.setVisible(true);
}
}

0 个答案:

没有答案