我的 Java 组件有问题。问题是在我的 GUI 中我看不到椭圆颜色的任何变化。当 OVF 标志设置为 false 时,它们应该是白色的,当标志 OVF 设置为 true 时,它们应该是红色的。但是当我开始我的程序时,标志 OVF 设置为 fasle 并且所有椭圆都是白色的 - 这很好。当标志变为真时,椭圆仍然是白色的。我尝试添加 repaint() 函数,但白色椭圆仍在闪烁,没有改变颜色。这是我的 Komponent 类代码:
import java.awt.*;
import javax.swing.*;
public class Komponent extends JComponent {
Counter counter3;
public Komponent() {
counter3 = new Counter();
}
@Override
public void paint(Graphics g) {
Graphics2D dioda = (Graphics2D) g;
int x1 = 85;
int x2 = 135;
int x3 = 35;
int x4 = 185;
int x5 = 235;
int x6 = 88;
int x7 = 138;
int x8 = 38;
int x9 = 188;
int x10 = 238;
int y_ob = 0;
int y = 3;
int width_ob = getSize().width / 9;
int height_ob = getSize().height - 1;
int width = (getSize().width / 9) - 6;
int height = (getSize().height - 1) - 6;
if (counter3.OVF == true) {
dioda.setColor(Color.BLACK);
dioda.fillOval(x1, y_ob, width_ob, height_ob);
dioda.fillOval(x2, y_ob, width_ob, height_ob);
dioda.fillOval(x3, y_ob, width_ob, height_ob);
dioda.fillOval(x4, y_ob, width_ob, height_ob);
dioda.fillOval(x5, y_ob, width_ob, height_ob);
dioda.setColor(Color.RED);
dioda.fillOval(x6, y, width, height);
dioda.fillOval(x7, y, width, height);
dioda.fillOval(x8, y, width, height);
dioda.fillOval(x9, y, width, height);
dioda.fillOval(x10, y, width, height);
repaint();
}
if (counter3.OVF == false) {
dioda.setColor(Color.BLACK);
dioda.fillOval(x1, y_ob, width_ob, height_ob);
dioda.fillOval(x2, y_ob, width_ob, height_ob);
dioda.fillOval(x3, y_ob, width_ob, height_ob);
dioda.fillOval(x4, y_ob, width_ob, height_ob);
dioda.fillOval(x5, y_ob, width_ob, height_ob);
dioda.setColor(Color.WHITE);
dioda.fillOval(x6, y, width, height);
dioda.fillOval(x7, y, width, height);
dioda.fillOval(x8, y, width, height);
dioda.fillOval(x9, y, width, height);
dioda.fillOval(x10, y, width, height);
repaint();
}
}
public static void main(String[] arg) {
new Komponent();
}
}
请帮忙(对不起我的英语);)