2010-10-26 41 views
2

我需要自定義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); 

} 
} 

感謝 喬蒂

+0

喬蒂看一看http://forums.sun.com/thread.jspa?threadID=422377 – 2010-10-26 06:35:08

回答

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

UIManager的解決方案僅在金屬LAF從什麼工作我可以告訴。

如果您想更改UI的行爲,那麼您需要更改UI。在這種情況下,您需要使用BasicSliderUI(或其子類之一)。然後我相信你需要重寫paintThumb()方法。

+0

+1這一直是我的經驗也是。參見http://stackoverflow.com/questions/2898259 – trashgod 2010-10-26 16:15:12

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) { 
     } 
    } 
}