如何删除jSlider的边框

时间:2013-11-29 12:11:45

标签: java swing border jslider

我定制了一个面临一个小问题的jSlider -

更改值后,我的自定义jSlider是显示的边框。对不起,我拍了照片,但我不允许发布。单击后,未定制的jSlider不会显示此边框。

是否有一个方法我必须覆盖才能摆脱这个边界?提前谢谢!

抱歉我的英文!希望有人能帮助我! 这是我的自定义用户界面:

    public class RedGreenSliderUI extends BasicSliderUI{
    Image knobImage ;
    private BasicStroke stroke = new BasicStroke(1f, BasicStroke.CAP_ROUND, 
            BasicStroke.JOIN_ROUND, 0f, new float[]{1f, 2f}, 0f);
    private static final float[] fractions = {0.0f, 0.5f};   
  private static final Color[] fillColors = {   
   /* new Color(0x2687AE),*/ 
   /* new Color(0x1658AE)*/ 
      new Color(0xc8041b),
      new Color(0xc8041b)
  };   
  private static final Color[] backColors = {   
    new Color(0x04c814),   
    new Color(0x04c814)   
  };   

    private static final Paint hFillGradient = new LinearGradientPaint(0, 0, 0, 11,   
          fractions, fillColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);   
  private static final Paint hBackGradient = new LinearGradientPaint(0, 0, 0, 11,   
          fractions, backColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);   
  private static final Paint vFillGradient = new LinearGradientPaint(0, 0, 11, 0,   
          fractions, fillColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);   
  private static final Paint vBackGradient = new LinearGradientPaint(0, 0, 11, 0,   
          fractions, backColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);   
  private static final Stroke roundEndStroke = new BasicStroke(5,   
          BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);   

        public RedGreenSliderUI( JSlider aSlider ) {

            super( aSlider );

            try {

                this.knobImage = ImageIO.read(getClass().getResource("ringelblumensalbe-             Updater-Grafiken/tongThumb01.png")); //.getClassLoader()


            } catch ( IOException e ) {

                e.printStackTrace();
            }
        }
        public void paintThumb(Graphics g)  {        

            g.drawImage( this.knobImage, thumbRect.x, thumbRect.y, 15, 30, null );

        }
         @Override
    protected Dimension getThumbSize() {
        return new Dimension(15, 30);
    }

         @Override
    protected Color getHighlightColor() {
             return new Color(98, 94, 0);
         }

          public void paintTrack(Graphics g) {   
    Graphics2D g2 = (Graphics2D) g;   
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,           RenderingHints.VALUE_ANTIALIAS_ON);   

    if (slider.getOrientation() == JSlider.HORIZONTAL) {   
      int cy = (trackRect.height / 2) - 2;   
      g.translate(trackRect.x, trackRect.y + cy);   

      g2.setStroke(roundEndStroke);   
      g2.setPaint(hBackGradient);   
      g2.drawLine(thumbRect.x, 2, trackRect.width, 2);   
      g2.setPaint(hFillGradient);   
      g2.drawLine(0, 2, thumbRect.x, 2);   

      g.translate(-trackRect.x, -(trackRect.y + cy));   
    } else {   
      int cx = (trackRect.width / 2) - 2;   
      g.translate(trackRect.x + cx, trackRect.y);   

      g2.setStroke(roundEndStroke);   
      g2.setPaint(vBackGradient);   
      g2.drawLine(2, 0, 2, thumbRect.y);   
      g2.setPaint(vFillGradient);   
      g2.drawLine(2, thumbRect.y, 2, trackRect.height);   

      g.translate(-(trackRect.x + cx), -trackRect.y);   
    }   
  }  
编辑:我知道了。答案是覆盖getFocusColor()。

@Override
     protected Color getFocusColor(){
         return new Color(255,255,255);
     }

3 个答案:

答案 0 :(得分:5)

焦点矩形由滑块的UI委托渲染,通常基于BasicSliderUI。虽然我不能提倡击败焦点指标,但您可以尝试以下方法之一:

  • 使用UIManager"Slider.focus"颜色设置为与"Slider.background"匹配。

  • 覆盖paintFocus()方法不执行任何操作;一个相关的例子是here

答案 1 :(得分:1)

Trashgod的答案很有效。对于像我这样的诺布,语法是;

UIManager.put("Slider.focus", UIManager.get("Slider.background"));

答案 2 :(得分:0)

您可以尝试使用border:0px;显示边界的特定类的属性。