java slider如何设置成可变的_Java Swing - 如何为JSlider设置自定义标签

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Graphics;

import java.awt.Polygon;

import java.util.Hashtable;

import javax.swing.Icon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JSlider;

public class Main {

public static void main(String args[]) {

String title = (args.length == 0 ? "Sample Slider" : args[0]);

JFrame f = new JFrame(title);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JSlider js1 = new JSlider();

js1.putClientProperty("JSlider.isFilled", Boolean.TRUE);

JSlider js2 = new JSlider();

js2.setMajorTickSpacing(25);

js2.setPaintTicks(true);

js2.setSnapToTicks(true);

JSlider js3 = new JSlider(JSlider.VERTICAL);

js3.setPaintTrack(false);

js3.setMinorTickSpacing(5);

js3.setMajorTickSpacing(10);

js3.setPaintTicks(true);

js3.setPaintLabels(true);

js3.setSnapToTicks(true);

JSlider js4 = new JSlider(JSlider.VERTICAL);

Hashtable table = new Hashtable();

table.put(new Integer(0), new JLabel(new DiamondIcon(Color.red)));

table.put(new Integer(10), new JLabel("Ten"));

table.put(new Integer(25), new JLabel("Twenty-Five"));

table.put(new Integer(34), new JLabel("Thirty-Four"));

table.put(new Integer(52), new JLabel("Fifty-Two"));

table.put(new Integer(70), new JLabel("Seventy"));

table.put(new Integer(82), new JLabel("Eighty-Two"));

table.put(new Integer(100), new JLabel(new DiamondIcon(Color.black)));

js4.setLabelTable(table);

js4.setPaintLabels(true);

js4.setSnapToTicks(true);

Container c = f.getContentPane();

c.add(js1, BorderLayout.NORTH);

c.add(js2, BorderLayout.SOUTH);

c.add(js3, BorderLayout.WEST);

c.add(js4, BorderLayout.EAST);

f.setSize(300, 200);

f.setVisible(true);

}

}

class DiamondIcon implements Icon {

private Color color;

private boolean selected;

private int width;

private int height;

private Polygon poly;

private static final int DEFAULT_WIDTH = 10;

private static final int DEFAULT_HEIGHT = 10;

public DiamondIcon(Color color) {

this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);

}

public DiamondIcon(Color color, boolean selected) {

this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);

}

public DiamondIcon(Color color, boolean selected, int width, int height) {

this.color = color;

this.selected = selected;

this.width = width;

this.height = height;

initPolygon();

}

private void initPolygon() {

poly = new Polygon();

int halfWidth = width / 2;

int halfHeight = height / 2;

poly.addPoint(0, halfHeight);

poly.addPoint(halfWidth, 0);

poly.addPoint(width, halfHeight);

poly.addPoint(halfWidth, height);

}

public int getIconHeight() {

return height;

}

public int getIconWidth() {

return width;

}

public void paintIcon(Component c, Graphics g, int x, int y) {

g.setColor(color);

g.translate(x, y);

if (selected) {

g.fillPolygon(poly);

} else {

g.drawPolygon(poly);

}

g.translate(-x, -y);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值