【发布时间】:2013-05-10 14:46:18
【问题描述】:
我的 Java 组件有问题。当我将 OVF 标志设置为 true 时,Rect 应该是红色(255,0,0),如果我将 OVF 标志设置为 false,Rect 应该是蓝色(0,0,255)。问题是我只能在我的 GUI 中看到蓝色矩形(即使 OVF 标志设置为 true)。我应该在此代码中更改什么?
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;
public class Komponent2 extends JComponent implements ActionListener
{
Timer tm = new Timer(10, this);
int x =30;
int y =0, y2 = 8;
Counter counter3;
Color Kolor = new Color(255,255,255);
public void paintComponent(Graphics g)
{
counter3=new Counter();
super.paintComponent(g);
g.setColor(Kolor);
g.fillRect(y,30,x,30);
tm.start();
}
public void actionPerformed(ActionEvent e)
{
if(y<0 || y>300)
y2=-y2;
y=y + y2;
if (counter3.OVF==true)
Kolor = new Color (255,0,0);
if (counter3.OVF==false)
Kolor = new Color (0,0,255);
repaint ();
}
}
谢谢你帮助我。
【问题讨论】:
-
您的代码需要紧急缩进。
-
可能与您在每次调用
paintComponent时如何实例化一个新的 counter3 有关。你在哪里设置 OVF? -
OVF 在 Counter 类中设置。当 MainReg 设置为 0 时,OVF 设置为 true(它可以工作,因为如果 OVF==true 我可以在我的 GUI 类标签“INTERRUPT”中看到
-
摆动计时器重复。 Y 默认,所以在paintComponent 中调用start 可能不是一个好主意
-
您可能想查看this 了解更多想法
标签: java swing colors jcomponent