1

それぞれが値の範囲を占める 3 種類の色 (たとえば、1 から 10 は緑、10 から 20 は黄色、20 から 30 は赤) を持つ JSlider を使用したいのですが、どのように実装できますか?

4

1 に答える 1

2

編集:

paintBackground()おっと、何らかの理由で JComponent にメソッドがあると思いました。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();
}
于 2012-04-30T21:13:22.460 に答える