![](/img/trans.png)
[英]Java: A defined paint method for my frame won't call repaint() when referenced from another class
[英]Java can't get paint method to repaint when a button is pressed
我目前正在制作摇滚剪刀游戏。 在编码的早期我遇到了一个问题,当按下按钮时我无法让代码重新绘制新的东西,尽管按钮正在工作并由代码注册。 这些按钮确实可以正常工作,因为它会打印出正在按下的内容,并且 y 的值也发生了变化,因此代码在某种程度上可以正常工作,但它没有正确执行。
public class RockPaperScissors implements ActionListener {
JButton rock =new JButton("Select rock");
JButton paper =new JButton("Select paper");
JButton scissors =new JButton("Select scissors");
public void frame() {
Gui gui = new Gui();
JFrame b = new JFrame("Rock paper scissors");
rock.setBounds(150,100,120,30);
paper.setBounds(350,100,120,30);
scissors.setBounds(550,100,120,30);
b.setSize(905,705);
b.setLocation(300,60);
b.setResizable(false);
b.setVisible(true);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setResizable(false);
b.setVisible(true);
b.add(rock);
b.add(paper);
b.add(scissors);
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
b.add(gui);
}
public static void main(String[] args) {
RockPaperScissors start = new RockPaperScissors();
start.frame();
}
@Override
public void actionPerformed(ActionEvent e) {
Gui selection = new Gui();
if (e.getSource() == rock){
selection.selector("rock");
} else if (e.getSource() == paper) {
selection.selector("paper");
} else if (e.getSource() == scissors) {
selection.selector("scissors");
}
}
这是 Gui 类,当然现在处理 gui 的任务只是在按下正确的按钮时绘制不同颜色的方块,但没有任何反应,我怀疑这可能是我遗漏的一个明显的解决方案。
public class Gui extends JPanel {
private int y;
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(50, 300, 300, 300);
g.setColor(Color.white);
g.fillRect(550, 300, 300, 300);
if (y == 1) {
g.setColor(Color.blue);
g.fillRect(50, 300, 300, 300);
}
if (y == 2) {
g.setColor(Color.black);
g.fillRect(50, 300, 300, 300);
}
if (y == 3) {
g.setColor(Color.yellow);
g.fillRect(50, 300, 300, 300);
}
}
public void selector(String x){
if (x == "paper"){
y = 1;
repaint();
System.out.println("paper" + y);
} else if (x == "rock") {
y = 2;
repaint();
System.out.println("rock" + y);
} else if (x == "scissors") {
y = 3;
repaint();
System.out.println("scissors" + y);
}
}
}
一个问题在这里:
public void actionPerformed(ActionEvent e) {
Gui selection = new Gui();
//....
selection.selector("rock");
是的,您正在更改 Gui 实例的状态,但它是错误的实例。 你已经有已显示的GUI实例,那就是其状态应该改变的实例。 您应该将该实例放在它自己的字段中,而不是将它埋在框架方法中。
所以改变
public class RockPaperScissors implements ActionListener {
JButton rock =new JButton("Select rock");
JButton paper =new JButton("Select paper");
JButton scissors =new JButton("Select scissors");
public void frame() {
Gui gui = new Gui();
至
public class RockPaperScissors implements ActionListener {
private JButton rock =new JButton("Select rock");
private JButton paper =new JButton("Select paper");
private JButton scissors =new JButton("Select scissors");
private Gui gui = new Gui();
public void frame() {
// Gui gui = new Gui();
并更改 gui字段的状态
另一个问题:不要使用==
或!=
比较字符串。 请改用equals(...)
或equalsIgnoreCase(...)
方法。 理解==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的重要内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.