[英]Java JComponent doesn't refresh
我的班級Component遇到問題。 問題是我的橢圓形沒有改變顏色。 函數if正在觀察Counter類中的OVF標志。 OVF = true時,橢圓應為紅色; OVF = false時,橢圓應為白色。 在我的GUI中,我只能看到紅色的橢圓形(即使OVF = false)。 我嘗試添加repaint()命令,但紅色橢圓形僅開始閃爍。 這是我的代碼:
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();
}
}
該代碼有什么問題?
如果應該是:
if (counter3.OVF == true) { // watch out for = and ==
// red
}
if (counter3.OVF == false) {
// white
}
或更簡單:
if (counter3.OVF) {
// red
} else {
// white
}
或最簡單:
dioda.setColor(counter3.OVF ? Color.RED : Color.WHITE);
dioda.fillOval(x1, y, width, height);
dioda.fillOval(x2, y, width, height);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.