1

我是 Java 新手,在实现 GUI 时需要您的帮助。下面是一个猜谜游戏代码。有用。

  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.Random;

  public class GuessGame extends JFrame {
     private JTextField guessTextField;
     private JLabel introLabel, guessLabel, clueLabel;
     private JButton enterB, playAgainB;

     private int randomNumber;

     public GuessGame() {
        super("Guessing Game!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Creates components
        enterB = new JButton("Guess");
        playAgainB = new JButton("Quit");
        introLabel = new JLabel("I have a number between 1 and 1000.");
        guessLabel = new JLabel("Can you guess my number? Please enter your guess:");

        clueLabel = new JLabel("");
       // comment2 = new JLabel(" ");
        guessTextField = new JTextField(5);

        //content pane
        Container c = getContentPane();
        setLayout(new FlowLayout());

        //adding component to the pane
        c.add(introLabel);
        c.add(guessLabel);
        c.add(guessTextField);
        //c.add(comment2);
        c.add(enterB);
        c.add(playAgainB);
        c.add(clueLabel);

        //enterB.setMnemonic('G');
        //playAgainB.setMnemonic('Q');

        setSize(350, 200);
        setLocationRelativeTo(null);
        setVisible(true);
        //setResizable(false);

        initializeNumber();

        //creating the handler
        GuessButtonHandler ghandler = new GuessButtonHandler(); //instantiate new object
        enterB.addActionListener(ghandler); // add event listener

        QuitButtonHandler qhandler = new QuitButtonHandler();
        playAgainB.addActionListener(qhandler);
     }

     private void initializeNumber() {
        randomNumber = new Random().nextInt(1000) + 1;
        System.out.println(randomNumber);
     }

     class QuitButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
           System.exit(0);
        }
     }

     class GuessButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
           int getUserInput;
           int diff;
           int Difference;
           try {
              getUserInput = Integer.parseInt(guessTextField.getText().trim());      

              if (getUserInput == randomNumber) {
                  clueLabel.setText("                              Correct!");
              }
              if (getUserInput > randomNumber) {
                 clueLabel.setText("                           Too High");
              } else {
                 clueLabel.setText("                           Too Low");
              }


           } 
           catch (NumberFormatException e1) {
              clueLabel.setText("Enter a VALID number!");
           }
        }


     }



     public static void main(String args[]) {
        //instantiate gueesgame object
         GuessGame app = new GuessGame();

     }
  }

但是,窗口的颜色应该变为红色或蓝色。请帮我处理这段代码。我是 Java 新手,它的语法。我真的很感谢你的帮助。谢谢!

4

2 回答 2

1

你可以使用:

current = Integer.parseInt(guessTextField.getText().trim()); 
if (!firstTime) {
    if (getUserInput > previous) {
        getContentPane().setBackground(Color.red);
    } else {
        getContentPane().setBackground(Color.blue);
    }
}

其中firstTimeprevious是类成员变量。

previous如果不满足getUserInput == randomNumber,不要忘记分配。

更新:

您将背景设置为蓝色两次:

if (getUserInput < randomNumber) {
   clueLabel.setText("Too Low");
   getContentPane().setBackground(Color.blue); <------ remove this extra call
   previous = getUserInput;
}
于 2012-08-26T16:27:41.977 回答
1

一个代码胜过千言万语......

 if (current_Input > previous) {
    c.setBackground(Color.red);
 } else {
    c.setBackground(Color.blue);
 }
于 2012-08-26T16:33:15.233 回答