English 中文(简体)
JSlider 准绳消失
原标题:JSlider ticks disappearing
public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Slider Example");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
            slider.setUI(new BasicSliderUI() {
                
                @Override
                public void paint(Graphics g, JComponent c) {
                    super.paint(g, c);
                }
                
                @Override
                public void paintTrack(Graphics g) {
                    if(slider.getOrientation() == JSlider.HORIZONTAL) {
                        int y = (trackRect.height / 2) - 2;
                        int w = trackRect.width;

                        g.translate(trackRect.x, trackRect.y + y);

                        g.setColor(Color.white);
                        
                        g.drawLine(0, -3, 0, 3);
                        g.drawLine(w -1, -3, w -1, 3);
                        
                        g.drawLine(0, 0, w -2, 0); // Main Line

                        g.translate(-trackRect.x, -(trackRect.y + y));
                    }
                }
                
                @Override
                public void paintThumb(Graphics g1) {
                    Rectangle knobBounds = thumbRect;
                    int x = knobBounds.x;
                    int y = knobBounds.y;
                    int w = knobBounds.width;
                    int h = knobBounds.height;

                    Graphics2D g = (Graphics2D) g1;
                    
                    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(Color.white);
                    g.translate(x, y);
                    
                    g.fillOval(0, 3, w, w);
                }
                
                @Override
                protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
                    g.setColor(Color.white);
                    g.drawLine(x, -5, x, tickBounds.height / 2 - 5);
                }
                
                @Override
                protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
                    g.setColor(Color.white);
                    g.drawLine(x, -5, x, tickBounds.height / 2 - 10);
                }
            });
            slider.setMajorTickSpacing(10);
            slider.setMinorTickSpacing(5);
            slider.setPaintTicks(true);
            slider.setPaintLabels(true);

            frame.getContentPane().add(slider);
            slider.setBackground(Color.black);
            frame.getContentPane().setBackground(Color.black);
            frame.setSize(400, 150);
            frame.setVisible(true);
        });
    }
}

我在《刑法》中曾遇到过一种奇怪的行为,当时我为我的国家情报和安全局专门安排了一次联合调查。 令人费解的是,当我rag笑 s时,标准是刻意抹去的。 然而,一旦我释放了umb,他们就会mag然地重新耳光。 难道有人会帮助我,这里会发生什么情况? 我曾寻求解决办法,但我无法找到解决办法。

问题回答

仍然可以找到问题的根源,但用在<条码>前的标尺(<> > > > 解决问题。

public void paint(Graphics g1, JComponent c) {
    paintTicks(g1);
    super.paint(g1, c);
}

在通常的<代码>JSlider中,标准没有通过改动改动而改变,因此<代码>p.Rect repainting没有按下文所示方式进行。

// BasicSliderUI
public void paint( Graphics g, JComponent c ) {
  //...
  if ( slider.getPaintTicks() && clip.intersects( tickRect ) ) {
    paintTicks( g ); }
  }

建议否决以下代码:,以便在 mo被撤回时重整整个<代码>JSlider。

class CustomSliderUI extends BasicSliderUI {
  @Override
  protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
    // ...
  }

  @Override
  protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
    // ...
  }

  @Override
  protected BasicSliderUI.TrackListener createTrackListener(JSlider slider) {
    return new TrackListener() {
      @Override
      public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
        e.getComponent().repaint();
      }
    };
  }
}

Main2.java

import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.plaf.basic.BasicSliderUI;

public class Main2 {
  public Component makeUI() {
    JSlider slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 50) {
      @Override public void updateUI() {
        super.updateUI();
        setUI(new CustomSliderUI());
      }
    };
    slider2.setMajorTickSpacing(10);
    slider2.setMinorTickSpacing(5);
    slider2.setPaintTicks(true);
    slider2.setPaintLabels(true);
    slider2.setBackground(Color.BLACK);

    JPanel p = new JPanel(new BorderLayout());
    p.setBackground(Color.BLACK);
    p.add(slider2);
    return p;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame("Slider Example");
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new Main2().makeUI());
      f.setSize(400, 150);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class CustomSliderUI extends BasicSliderUI {
  @Override
  public void paintTrack(Graphics g) {
    if (slider.getOrientation() == JSlider.HORIZONTAL) {
      int y = (trackRect.height / 2) - 2;
      int w = trackRect.width;
      g.translate(trackRect.x, trackRect.y + y);
      g.setColor(Color.white);
      g.drawLine(0, -3, 0, 3);
      g.drawLine(w - 1, -3, w - 1, 3);
      g.drawLine(0, 0, w - 2, 0); // Main Line
      g.translate(-trackRect.x, -(trackRect.y + y));
    }
  }

  @Override
  public void paintThumb(Graphics g1) {
    Rectangle knobBounds = thumbRect;
    int x = knobBounds.x;
    int y = knobBounds.y;
    int w = knobBounds.width;
    int h = knobBounds.height;
    Graphics2D g = (Graphics2D) g1;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.white);
    g.translate(x, y);
    g.fillOval(0, 3, w, w);
  }

  @Override
  protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
    g.setColor(Color.white);
    g.drawLine(x, -5, x, tickBounds.height / 2 - 5);
  }

  @Override
  protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
    g.setColor(Color.white);
    g.drawLine(x, -5, x, tickBounds.height / 2 - 10);
  }

  @Override
  protected BasicSliderUI.TrackListener createTrackListener(JSlider slider) {
    return new TrackListener() {
      @Override
      public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
        e.getComponent().repaint();
      }
    };
  }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签