Custom Java Swing Meter Control

后端 未结 2 638
自闭症患者
自闭症患者 2021-01-15 17:09

I\'m trying to make a custom swing control that is a meter. Swing Meter http://dl.dropbox.com/u/2363305/Programming/Java/swing_meter.gif

The arrow will move up and d

相关标签:
2条回答
  • 2021-01-15 17:12

    You could use a JSlider and use setValue(int n) to set the value whenever you need. You can also change the default appearance, so you get an arrow and a gradient as you want.

    0 讨论(0)
  • 2021-01-15 17:26

    In addition to @Jonas' example, you might like to look at the article How to Write a Custom Swing Component.

    Addendum: On reflection, it looks a little intimidating, but you can extend BasicSliderUI and reuse some of your code in paintThumb() and paintTrack().

    JSlider slider = new JSlider();
    slider.setUI(new MySliderUI(slider));
    ...
    private static class MySliderUI extends BasicSliderUI {
    
        public MySliderUI(JSlider b) {
            super(b);
        }
    
        @Override
        public void paintTrack(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Rectangle r = trackRect;
            g2d.setPaint(new GradientPaint(
                r.x, r.y, Color.red, r.x + r.width, r.y + r.height, Color.blue));
            g.fillRect(r.x, r.y, r.width, r.height);
        }
    
        @Override
        public void paintThumb(Graphics g) {
            super.paintThumb(g); // replace with your fill() 
        }
    }
    
    0 讨论(0)
提交回复
热议问题