1

不知道为什么我的代码中的updateMeter()方法目前不起作用,它调用gauge.setValueAnimated(x),x值发生变化,但量规没有变化?谁能告诉我如何不断更新仪表的值?

这是我的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import eu.hansolo.steelseries.gauges.Radial;

public class TestGauge extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    private int x1;
    private int y1;
    Timer timer;
    Radial gauge = new Radial();

    public TestGauge() {
        createMeter();
        timer = new Timer(10, this);
        timer.start();
    }
    private void createMeter() {
        gauge.setTitle("Demo title");
        gauge.setUnitString("Some units");
        this.setLayout(new BorderLayout());
        this.add(gauge, BorderLayout.CENTER);
        gauge.setValueAnimated(10);
    }
    private void updateMeter(int x) {
        if (x > 100) {
            x = 100;
        }
        System.out.println(x);
        this.gauge.setValueAnimated(x);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.scale(1, 1);
        g2d.setPaint(Color.RED);
        g2d.fill(new Rectangle2D.Double(getX1(), getY1(), 20, 20));
    }
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        setX1(getX1() + 1);
        updateMeter(getX1());
        repaint();
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("test");
        TestGauge test = new TestGauge();
        test.setVisible(true);
        test.setBackground(Color.WHITE);
        frame.add(test, BorderLayout.NORTH);
        frame.pack();
        frame.setFocusable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public int getX1() {
        return x1;
    }
    public void setX1(int x1) {
        this.x1 = x1;
    }
    public int getY1() {
        return y1;
    }
    public void setY1(int y1) {
        this.y1 = y1;
    }
}
4

0 回答 0