2

在覆盖我的 JSlider 的 paintTrack 和 paintThumb 方法后,如果我拖动它的拇指,它就会滞后。

我已经用图像替换了轨道和拇指。任何想法如何解决这个问题?

public class test {
private static JFrame frame;
private static JSlider slider;

public static void main(String[] args) throws IOException {
    frame = new JFrame();
    slider = new JSlider();
    slider.setUI(new MySliderUI(slider));
    frame.add(slider);
    frame.setVisible(true);
    frame.pack();
    frame.setSize(1200, 720);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private static class MySliderUI extends BasicSliderUI {

    private BufferedImage imgTrack;
    private BufferedImage imgThumb;

    public MySliderUI(JSlider slider) throws IOException {
        super(slider);
        imgTrack = ImageIO.read(new File("icon/track.png"));
        imgThumb = ImageIO.read(new File("icon/thumb.png"));

    }

    @Override
    public void paintTrack(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Rectangle t = trackRect;
        g2d.drawImage(imgTrack, t.x, t.y, t.width, t.height, null);
    }

    @Override
    public void paintThumb(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_OFF);
        Rectangle t = thumbRect;
        g2d.drawImage(imgThumb, t.x, t.y, null);
    }

}

}

编辑:解决了,我还必须覆盖 getThumbSize()

@Override
    protected Dimension getThumbSize() {
        return new Dimension(imgThumb.getWidth(), imgThumb.getHeight());
    }
4

0 回答 0