简体   繁体   English

定制设计JScollPane Java Swing

[英]Custom design JScollPane Java Swing

I need to design this scrollbar for all my scrollpanes : 我需要为我的所有scrollpanes设计这个滚动条:

在此输入图像描述

With Java Swing. 使用Java Swing。 I am using Netbeans IDE. 我正在使用Netbeans IDE。 What is the simplest solution ? 什么是最简单的解决方案?

Thank you very much. 非常感谢你。

Regards 问候

You can customize the look of a Swing component by setting a custom UI . 您可以通过设置自定义UI来自定义 Swing组件的外观 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 . 其中MyScrollBarUI派生自javax.swing.plaf.basic.BasicScrollBarUI To do this for all scroll bars (not only in JScrollPane instances), call 要对所有滚动条(不仅在JScrollPane实例中)执行此操作,请调用

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

before you create any Swing components. 在创建任何Swing组件之前。

In MyScrollBarUI , you override the following methods: MyScrollBarUI ,您重写以下方法:

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. 您的滚动条图形非常简单,因此实现起来应该不会太难。

1) override JToolBar 1)覆盖JToolBar

2) most of Custom Look and Feel overrode that 2)大多数自定义外观都超过了它

Don't forget that if you plan to use the UIManager to override all scrollbars like this 不要忘记,如果您打算使用UIManager覆盖所有这样的滚动条

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

then your SimpleScrollBarUI class must have the createUI method, ie: 那么你的SimpleScrollBarUI类必须有createUI方法,即:

public class SimpleScrollBarUI extends BasicScrollBarUI {

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

    //...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM