简体   繁体   English

Java JComponent不刷新

[英]Java JComponent doesn't refresh

I've got problem with my class Component. 我的班级Component遇到问题。 The problem is that my ovals doesn't changing their colors. 问题是我的椭圆形没有改变颜色。 The function if is observing OVF flag in class Counter. 函数if正在观察Counter类中的OVF标志。 when OVF=true ovals should be red, and when OVF=false ovals should be white. OVF = true时,椭圆应为红色; OVF = false时,椭圆应为白色。 in my GUI i can see only red ovals (even if OVF=false). 在我的GUI中,我只能看到红色的椭圆形(即使OVF = false)。 I try to add repaint() command but red ovals only started blinking. 我尝试添加repaint()命令,但红色椭圆形仅开始闪烁。 Here is my code: 这是我的代码:

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();
}
}

what is wrong with that code? 该代码有什么问题?

If should be: 如果应该是:

if (counter3.OVF == true) { // watch out for = and ==
    // red
}
if (counter3.OVF == false) {
    // white
}

Or simpler: 或更简单:

if (counter3.OVF) {
    // red
} else {
    // white
}

Or simplest: 或最简单:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM