0

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

请帮忙(对不起我的英语);)

4

2 回答 2

3
  1. override getPreferredSize,那么坐标将是getHeight/Weight

  2. Swing 中的自定义绘画是通过使用public void paintComponent(Graphics g) {而不是 public完成的void paint(Graphics g) {

  3. 里面的第一行代码paintComponent应该是super.paintComponent(g);

  4. 如果需要,开始Swing Timer定期repaint()

  5. 没有理由在运行时创建所有元素,将所有元素放入array(在您的情况下为两个数组),paintComponent仅在数组内循环

编辑(其余由您决定,第 1 点和第 5 点)

在此处输入图像描述. .在此处输入图像描述

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Komponent {

    private boolean counter3 = false;
    private JFrame frame = new JFrame();
    private Timer timer;
    private CustomComponents cc1 = new CustomComponents();

    public Komponent() {
        counter3 = false;
        frame = new JFrame();
        frame.setLayout(new GridLayout(1, 1, 10, 10));
        frame.add(cc1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
        timer = new javax.swing.Timer(1500, updateCol());
        timer.setRepeats(true);
        timer.start();
    }

    private Action updateCol() {
        return new AbstractAction("Hello World") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bol = counter3;
                if (bol) {
                    counter3 = false;
                    cc1.repaint();
                } else {
                    counter3 = true;
                    cc1.repaint();
                }
            }
        };
    }

    class CustomComponents extends JComponent {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getMinimumSize() {
            return new Dimension(100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 200);
        }

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(300, 300);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(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) {
                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);
            } else {
                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);
            }
            dioda.dispose();
        }
    }

    public static void main(String[] arg) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Komponent komponent = new Komponent();
            }
        });
    }
}
于 2013-05-15T12:01:04.833 回答
2

你在你的paint()方法中调用repaint()?它再次调用paint() 方法,它可能再次调用repaint(),依此类推....

不确定,如果这是您的问题,但我认为您在错误的地方调用重绘。

于 2013-05-15T11:56:57.663 回答