我遇到了類組件問題。問題是我的橢圓不改變他們的顏色。函數if在類Counter中觀察OVF標誌。當OVF =真橢圓應該是紅色的,並且當OVF =假橢圓應該是白色的。在我的GUI中,我只能看到紅色的橢圓(即使OVF = false)。我嘗試添加重繪()命令,但紅色橢圓只開始閃爍。這裏是我的代碼:Java JComponent不刷新
import java.awt.*;
import javax.swing.*;
import java.util.Observable;
public class Komponent extends JComponent
{
Counter counter3;
public Komponent()
{
counter3=new Counter();
}
public void paint(Graphics g)
{
Graphics2D dioda = (Graphics2D)g;
int x1 = 85;
int x2 = 135;
int y = 3;
int width = (getSize().width/9)-6;
int height = (getSize().height-1)-6;
if (counter3.OVF = true)
{
dioda.setColor(Color.RED);
dioda.fillOval(x1, y, width, height);
dioda.fillOval(x2, y, width, height);
}
if (counter3.OVF = false)
{
dioda.setColor(Color.WHITE);
dioda.fillOval(x1, y, width, height);
dioda.fillOval(x2, y, width, height);
}
}
public static void main(String[] arg)
{
new Komponent();
}
}
該代碼有什麼問題?
不覆蓋塗料,其建議您使用的paintComponent代替。你也應該調用super.paintComponent(或者在你的情況下是super.paint)。查看[自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/)以獲取更多詳細信息 – MadProgrammer
爲了更快獲得更好的幫助,請發佈[SSCCE](http://sscce.org/) 。 –