Custom design JScollPane Java Swing

僤鯓⒐⒋嵵緔 提交于 2019-12-17 10:31:14

问题


I need to design this scrollbar for all my scrollpanes :

With Java Swing. I am using Netbeans IDE. What is the simplest solution ?

Thank you very much.

Regards


回答1:


You can customize the look of a Swing component by setting a custom UI. In the case of a scroll pane's scroll bar, you do

scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());

where MyScrollBarUI is derived from javax.swing.plaf.basic.BasicScrollBarUI. To do this for all scroll bars (not only in JScrollPane instances), call

UIManager.put("ScrollBarUI", "my.package.MyScrollBarUI");

before you create any Swing components.

In MyScrollBarUI, you override the following methods:

public class MyScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
        // your code
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        // your code
    }
}

Your scroll bar is graphically very simple, so it should not be too hard to implement.




回答2:


1) override JToolBar

2) most of Custom Look and Feel overrode that




回答3:


Don't forget that if you plan to use the UIManager to override all scrollbars like this

UIManager.put("ScrollBarUI", "my.custom.SimpleScrollBarUI");

then your SimpleScrollBarUI class must have the createUI method, i.e:

public class SimpleScrollBarUI extends BasicScrollBarUI {

    public static ComponentUI createUI(JComponent c) {
        return new SimpleScrollBarUI();
    }

    //...

}


来源:https://stackoverflow.com/questions/8208508/custom-design-jscollpane-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!