如何隐藏jSlider的旋钮?

时间:2010-10-26 06:25:40

标签: java swing jslider

我需要自定义JSlider的旋钮。我需要将自己的旋钮图像放在Jslider的默认旋钮上。 问题是目前有两个旋钮正在响应。一个我自己的旋钮和第二个默认旋钮。请告诉我如何隐藏默认旋钮或任何其他解决方案。

以下代码用于执行此操作。

public class ImageTest {

 JSlider slider;
 JLabel label;

 public ImageTest()
 {

  JPanel panel = new BackgroundPanel();

  slider = new BackgroundSlider();

  slider.setMaximum(300);
  slider.setMinimum(0);
  slider.setValue(50);

  slider.setExtent(10);
  slider.addChangeListener(new MyChangeAction());
  label = new JLabel("50");
  panel.setLayout(new GridBagLayout());

  panel.setSize(797,402);
  slider.setOpaque(false);
  slider.setPaintTrack(false);

  label.setOpaque(false);

  slider.setPreferredSize(new Dimension(340, 20));

  GridBagConstraints gridBagConstraintsSlider = new GridBagConstraints();
  gridBagConstraintsSlider.gridy = 0;
  gridBagConstraintsSlider.gridx = 0;
  gridBagConstraintsSlider.fill = GridBagConstraints.HORIZONTAL;
  gridBagConstraintsSlider.insets = new Insets(0, 283, 260, 0);


  GridBagConstraints gridBagConstraints = new GridBagConstraints();
  gridBagConstraints.gridy = 0;
  gridBagConstraints.gridx = 1;
  gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
  gridBagConstraints.insets = new Insets(0, 50, 240, 0);

  panel.add(slider, gridBagConstraintsSlider);
  panel.add(label, gridBagConstraints);

  JFrame frame = new JFrame();
  frame.getContentPane().add(panel);
  frame.setSize(797,402);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  WindowUtil.locateCenter(frame);
 }

 public static void main(String[] args) {
  ImageTest im= new ImageTest();
 }

 public class MyChangeAction implements ChangeListener{
  public void stateChanged(ChangeEvent ce){
   int value = slider.getValue();
   String str = Integer.toString(value);
   label.setText(str);
   if(value==300)
   {
    label.setText("Max");
   }

  }
 }

}



class BackgroundSlider extends JSlider
{
 Image image;
 public BackgroundSlider()
 {
  try
  {
   image = javax.imageio.ImageIO.read(new File("slider.png"));
  }
  catch (Exception e) { /*handled in paintComponent()*/ }
 }

 protected void paintComponent(Graphics g)
 {
  super.paintComponent(g); 
  if (image != null)
   g.drawImage(image, this.getValue(),(int)this.getAlignmentY(),10,20,this);


  g.setColor(Color.RED); 
  //draw a centered horizontal line 
  g.drawRect(15,this.getHeight()-1,this.getValue(),this.getHeight()+2); 
  g.fillRect(15,this.getHeight()-1,this.getValue(),this.getHeight()+2); 

 }
}

由于 乔蒂

3 个答案:

答案 0 :(得分:2)

要隐藏旋钮,请使用空白图标覆盖UIManager的Slider.horizontalThumbIcon属性,如下所示:

public static void main(String[] args) throws Exception {

    UIManager.getLookAndFeelDefaults().put("Slider.horizontalThumbIcon",new Icon(){
        @Override
        public int getIconHeight() {
            return 0;
        }
        @Override
        public int getIconWidth() {
            return 0;
        }
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            //do nothing
        }
    });

    JFrame f = new JFrame();
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
    slider.setMajorTickSpacing(10);
    slider.setMinorTickSpacing(1);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);

    f.add(slider);
    f.setSize(200,200);
    f.setVisible(true);

}

答案 1 :(得分:1)

UIManager解决方案仅适用于我所知道的Metal LAF。

如果您想更改UI的行为,则需要更改UI。在这种情况下,您需要BasicSliderUI(或其子类之一)。然后我相信你需要覆盖paintThumb()方法。

答案 2 :(得分:1)

具有不同BasicSliderUI的解决方案如下所示:

public class SuperSlider extends JSlider {
    public SuperSlider(int min, int max, int value) {
        super(min,max,value);
        setUI(new SuperSliderUI(this));
    }
    private class SuperSliderUI extends BasicSliderUI {
        @Override
        public void paintThumb(Graphics g) {
        }
    }
}