Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JComponent不';t刷新_Java_Swing_Colors_Components_Jcomponent - Fatal编程技术网

Java JComponent不';t刷新

Java JComponent不';t刷新,java,swing,colors,components,jcomponent,Java,Swing,Colors,Components,Jcomponent,我的类组件有问题。问题是我的卵形不会改变它们的颜色。函数if正在类计数器中观察OVF标志。当OVF=真卵形时应为红色,当OVF=假卵形时应为白色。在我的GUI中,我只能看到红色的椭圆形(即使OVF=false)。我尝试添加repaint()命令,但红色椭圆只开始闪烁。这是我的密码: import java.awt.*; import javax.swing.*; import java.util.Observable; public class Komponent extends JComp

我的类组件有问题。问题是我的卵形不会改变它们的颜色。函数if正在类计数器中观察OVF标志。当OVF=真卵形时应为红色,当OVF=假卵形时应为白色。在我的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);

谢谢,你说得对。但是现在椭圆仍然是白色的,也许我应该添加repaint()函数。您的答案都很好,谢谢:)但我仍然只能看到白色的椭圆:(@user2283763 try
System.out.println(counter3.OVF)
paint
中,检查
计数器3.OVF
的值。我已经得到了它,当我计数并且它溢出时,标志OVF被设置为True不覆盖paint,建议您改用paintComponent。您也应该被称为super.paintComponent(或者super.paint)。查看更多详细信息。若要更快获得更好的帮助,请发布一个。