大家好。我有一个 JPanel,它在单击时会改变颜色(这在另一个类中得到了正确处理)。
不幸的是,当我调用 repaint() 方法时,它不会绘制(或者它使用 var currentBGColor 的旧 Color 值调用 paintComponent 方法 -> 请参见下面的代码)
public class MyClass extends JPanel {
curentBGColor = Color.red;
final int SIZE = 70;
public MyClass (){
setPreferredSize (new Dimension (SIZE,SIZE));
}
public void paintComponent (Graphics g)
{
g.setColor (currentBGColor); //I want this to paint white when newColor() is called
g.fillRect (0,0,getWidth(),getHeight());
g.setColor (Color.black);
g.drawLine (0,0,SIZE-1,0);
g.drawLine (0,0,0,SIZE-1);
g.drawLine (0,SIZE-1,SIZE-1,SIZE-1);
g.drawLine (SIZE-1,0,SIZE-1,SIZE-1);
}
void newColor (){
currentBGColor = Color.white;
repaint ();
revalidate();
}
}
有谁知道为什么它没有用新颜色绘画?