改变JSlider的外观和感觉(Change the JSlider look and feel)

2019-07-31 10:17发布

我已经通过这个网站去改变外观和感觉的JSlider ,但除了Slider.altTrackColor没有别的工作。 我想要做的事一样显示在PIC1任何建议将是很大的帮助。 我工作的JDK 1.6。

UIDefaults defaults = UIManager.getDefaults();    

       defaults.put("Slider.altTrackColor", Color.red);
       defaults.put("Slider.thumb", Color.red);

我也试过这样:

    WindowUtilities.setNativeLookAndFeel();

 // WindowUtilities.setNimbuzzLookAndFeel();

 // WindowUtilities.setJavaLookAndFeel();

WindowUtilities是一流的,告诉系统使用本机的外观和感觉,因为在以前的版本。 金属(Java)的LAF否则默认。

public static void setNativeLookAndFeel() {
try {
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
  System.out.println("Error setting native LAF: " + e);
}

Answer 1:

这里是第一个图像上显示的工作UI的一个完整的例子:
(没有魔法 - 只是一些工作,在图形和基本的UI知识)

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicSliderUI;

/**
 *
 * @see http://stackoverflow.com/a/12297384/714968
 */
public class CustomSliderUI extends BasicSliderUI {

    private BasicStroke stroke = new BasicStroke(1f, BasicStroke.CAP_ROUND, 
            BasicStroke.JOIN_ROUND, 0f, new float[]{1f, 2f}, 0f);

    public CustomSliderUI(JSlider b) {
        super(b);
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        super.paint(g, c);
    }

    @Override
    protected Dimension getThumbSize() {
        return new Dimension(12, 16);
    }

    @Override
    public void paintTrack(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Stroke old = g2d.getStroke();
        g2d.setStroke(stroke);
        g2d.setPaint(Color.BLACK);
        if (slider.getOrientation() == SwingConstants.HORIZONTAL) {
            g2d.drawLine(trackRect.x, trackRect.y + trackRect.height / 2, 
                    trackRect.x + trackRect.width, trackRect.y + trackRect.height / 2);
        } else {
            g2d.drawLine(trackRect.x + trackRect.width / 2, trackRect.y, 
                    trackRect.x + trackRect.width / 2, trackRect.y + trackRect.height);
        }
        g2d.setStroke(old);
    }

    @Override
    public void paintThumb(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        int x1 = thumbRect.x + 2;
        int x2 = thumbRect.x + thumbRect.width - 2;
        int width = thumbRect.width - 4;
        int topY = thumbRect.y + thumbRect.height / 2 - thumbRect.width / 3;
        GeneralPath shape = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
        shape.moveTo(x1, topY);
        shape.lineTo(x2, topY);
        shape.lineTo((x1 + x2) / 2, topY + width);
        shape.closePath();
        g2d.setPaint(new Color(81, 83, 186));
        g2d.fill(shape);
        Stroke old = g2d.getStroke();
        g2d.setStroke(new BasicStroke(2f));
        g2d.setPaint(new Color(131, 127, 211));
        g2d.draw(shape);
        g2d.setStroke(old);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JSlider slider = new JSlider(0, 100);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.setMinorTickSpacing(5);
        slider.setMajorTickSpacing(25);
        slider.setUI(new CustomSliderUI(slider));
        frame.add(slider);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

您可以轻松地通过改变返回的边界修改拇指(夹具)的大小。 拇指画会适应任何尺寸。 而且你还可以轻松地定制任何其他滑块部分的画。

这里顺便最终观点:

PS我没有适应UI的垂直滑块,但不应该花太长时间。



文章来源: Change the JSlider look and feel