gpt4 book ai didi

java - 出现在同一帧

转载 作者:行者123 更新时间:2023-12-04 05:34:35 27 4
gpt4 key购买 nike

我希望两个不同的图像出现在两个不同的帧中。问题是这段代码不显示这两个图像(圆圈),而只显示最后一个。任何帮助将不胜感激!谢谢。

public class MyCanvas extends JPanel {


private static final long serialVersionUID = 1L;
static int paint=0;

public MyCanvas(){
}

public void paintComponent(Graphics graphics){


System.out.println("mpika!!!");
// super.paintComponent(graphics);
if(paint==0){
graphics.setColor(Color.blue);
graphics.drawOval(250,250,250,250);
}
else{
graphics.setColor(Color.red);
graphics.drawOval(150,150,150,150);
}
}


public static void other(){
JFrame frame2 = new JFrame();
MyCanvas canvas2 = new MyCanvas();
frame2.setSize(700, 700);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(canvas2);
frame2.setVisible(true);
Graphics graph2 = canvas2.getGraphics();
canvas2.paintComponent(graph2);

}
public static void main(String[] args){
double t;
JFrame frame = new JFrame();
MyCanvas canvas = new MyCanvas();
frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(canvas);
frame.setVisible(true);
Scanner input = new Scanner(System.in);
Graphics graph = canvas.getGraphics();
canvas.paintComponent(graph);
// t = input.nextInt();

paint=1;
other();

}
}

最佳答案

你从不打电话setVisibleframe2 .

也为 paint是静态的:

静态 int 油漆 = 0;

你只会看到一种颜色。

解决办法是把它变成MyCanvas中的成员变量,类似于:

public void setColorFlag(int color)

或者最好还是传入圆圈颜色(!)。

关于java - 出现在同一帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132157/

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