gpt4 book ai didi

java - GUI - 更改 JFrame 的颜色

转载 作者:行者123 更新时间:2023-11-30 04:41:48 25 4
gpt4 key购买 nike

我是 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 及其语法的新手。我非常感谢你的帮助。谢谢!

最佳答案

您可以使用:

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

其中 firstTimeprevious 是类成员变量。

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

更新:

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

if (getUserInput < randomNumber) {
clueLabel.setText("Too Low");
getContentPane().setBackground(Color.blue); <------ remove this extra call
previous = getUserInput;
}

关于java - GUI - 更改 JFrame 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12131723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com