【问题标题】:color java JSlider based on tick value基于刻度值的颜色java JSlider
【发布时间】:2012-04-30 20:37:17
【问题描述】:

我想要一个 JSlider 有三种颜色,每种颜色占一个范围的值(例如,1 到 10 是绿色,10 到 20 是黄色,20 到 30 是红色),如何实现?

【问题讨论】:

  • 你想让背景有三色条纹吗?还是您希望根据所选值将其着色为三种颜色中的一种?
  • @Russell Zahniser:我希望背景有三条颜色,我怎样才能让 GradientPaint 对背景 JPanel 做到这一点

标签: java swing colors jslider


【解决方案1】:

编辑:

糟糕,出于某种原因,我认为 JComponent 中有一个 paintBackground() 方法。我猜你应该改为setOpaque(false)(这样super 就不会绘制背景),然后像这样覆盖paintComponent()

protected void paintComponent(Graphics g) {
   int w = getWidth();
   int h = getHeight();
   int x1 = w / 3;
   int x2 = w * 2 / 3;

   g.setColor(Color.GREEN);
   g.fillRect(0, 0, x1, h)
   g.setColor(Color.YELLOW);
   g.fillRect(x1, 0, x2 - x1, h)
   g.setColor(Color.RED);
   g.fillRect(x2, 0, w - x2, h)

   super.paintComponent();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2021-04-05
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多