我正在尝试编写一个程序,您可以在其中猜测随机生成的数字(从1-1000开始),程序会告诉您是否接近。我遇到的问题是我必须根据你对答案的接近程度来改变背景颜色。红色更近,蓝色更远。我有代码,但我无法弄清楚为什么背景不起作用。这与容器有关吗?谢谢!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;;
public class GuessGame 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 randomNumber;
private int userGuess;
private int counter = 0;
private int lastGuess = 0;
private Color background;
Container container;
public GuessGame()
{
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 my number?");
enterLabel = new JLabel("Please enter your first guess.");
userMessageLabel = new JLabel("");
randomNumber = new Random().nextInt(1000) + 1;
container=getContentPane();
container.setLayout(new FlowLayout());
container.add(initialTextLabel);
container.add(enterLabel);
container.add(guessBox);
container.add(newGameButton);
container.add(enterButton);
container.add(exitButton);
container.add(userMessageLabel);
setSize(400, 150);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
newGameButtonHandler nghandler = new newGameButtonHandler();
newGameButton.addActionListener(nghandler);
ExitButtonHandler exithandler = new ExitButtonHandler();
exitButton.addActionListener(exithandler);
enterButtonHandler enterhandler = new enterButtonHandler();
enterButton.addActionListener(enterhandler);
}
class newGameButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
getContentPane();
background=Color.lightGray;
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 paint(Graphics g)
{
super.paint(g);
container.setBackground(background);
}
public void compareGuess(int userGuess, int randomNumber)
{
counter++;
if (userGuess == randomNumber)
{
userMessageLabel.setText("You are correct, it took you: " + counter + " tries");
getContentPane();
background=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))
{
getContentPane();
background=Color.red;
}
else if ((randomNumber - userGuess) < (randomNumber - lastGuess))
{
getContentPane();
background=Color.blue;
}
else
{
getContentPane();
background=Color.gray;
}
}
lastGuess = userGuess;
}
public static void main(String[] args)
{
GuessGame myGuessGame = new GuessGame();
myGuessGame.setVisible(true);
}
}
答案 0 :(得分:3)
您正在尝试覆盖paint(Graphics g)
的{{1}}功能。我们不应该覆盖顶级组件的绘制功能,如JFrame
。
使用扩展JFrame
的自定义组件并覆盖其JPanel
函数,不要忘记在此函数中调用paintComponent(Graphics g)
。
如果要对组件的绘制进行任何更新,请在该组件上调用super.paintComponent(g)
以反映GUI上的更改。
请访问官方教程页面:Lesson: Performing Custom Painting
答案 1 :(得分:2)
除了Sage的评论之外,您还在setBackground
内拨打paint
,这只是向paint
再次发出请求......一次又一次......
另一个问题是,您正在更改框架的背景,而不是内容窗格。
相反,摆脱你的paint
方法,你没有做任何事情......
相反,当您想要更改颜色时,只需拨打getContentPane().setBackground(...)
,例如......
class newGameButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
getContentPane().setBackground(Color.lightGray);
userMessageLabel.setText("");
randomNumber = new Random().nextInt(1000) + 1;
}
}
如果不起作用,您可能需要致电getContentPane().repaint()