更改Swing应用程序上所有按钮的光标

时间:2012-08-18 05:30:02

标签: java swing cursor jbutton jcomponent

我有一个带有主框架的Swing应用程序,其中包含一些其他形式。我需要实现一种通用方法来为任何表单上的所有按钮设置手形光标。

这与我们在网页上使用css(input[type=button] { cursor:pointer; }

的做法类似

2 个答案:

答案 0 :(得分:6)

如果要动态地和/或在特定表单上更改光标,像@Madprogrammer建议的那样走树就是 方法。

只是为了好玩(并再次展示SwingX :) - 如果你想全局安装那个游标然后不关心,安装一个负责它的ui-delegate。在SwingX中,它就像实现和插入自定义按钮插件一样简单。副作用与其他答案相同(虽然不能解决)。通常的缺点(就像安装自定义ui委托时一样)需要为所有LAF创建子类和插件委托。

public class ButtonCursorAddon extends AbstractComponentAddon {

    /**
     * @param name
     */
    protected ButtonCursorAddon() {
        super("RolloverCursor");
    }

    @Override
    protected void addBasicDefaults(LookAndFeelAddons addon,
            DefaultsList defaults) {
        UIManager.getDefaults().remove("ButtonUI");
        defaults.add("ButtonUI", "org.jdesktop.swingx.plaf.ButtonCursorAddon$BasicButtonCursorUI");
    }

    @Override
    protected void addMetalDefaults(LookAndFeelAddons addon,
            DefaultsList defaults) {
        UIManager.getDefaults().remove("ButtonUI");
        defaults.add("ButtonUI", "org.jdesktop.swingx.plaf.ButtonCursorAddon$MetalButtonCursorUI");
    }

    @Override
    protected void addWindowsDefaults(LookAndFeelAddons addon,
            DefaultsList defaults) {
        UIManager.getDefaults().remove("ButtonUI");
        defaults.add("ButtonUI", "org.jdesktop.swingx.plaf.ButtonCursorAddon$WindowsButtonCursorUI");
    }


    @Override
    protected void addNimbusDefaults(LookAndFeelAddons addon,
            DefaultsList defaults) {
        UIManager.getDefaults().remove("ButtonUI");
        defaults.add("ButtonUI", "org.jdesktop.swingx.plaf.ButtonCursorAddon$SynthButtonCursorUI");
    }


    public static class BasicButtonCursorUI extends BasicButtonUI {

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

        @Override
        protected BasicButtonListener createButtonListener(AbstractButton b) {
            return new BasicHyperlinkListener(b);
        }

    }

    public static class SynthButtonCursorUI extends SynthButtonUI {

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

        @Override
        protected BasicButtonListener createButtonListener(AbstractButton b) {
            return new BasicHyperlinkListener(b);
        }

    }

    public static class MetalButtonCursorUI extends MetalButtonUI {

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

        @Override
        protected BasicButtonListener createButtonListener(AbstractButton b) {
            return new BasicHyperlinkListener(b);
        }

    }

    public static class WindowsButtonCursorUI extends WindowsButtonUI {

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

        @Override
        protected BasicButtonListener createButtonListener(AbstractButton b) {
            return new BasicHyperlinkListener(b);
        }

    }

}

// usage: plug-in once in your application code (before creating any buttons)
static {
    LookAndFeelAddons.contribute(new ButtonCursorAddon());
}
  

我收到此错误:UIDefaults.getUI()失败:没有ComponentUI类

适用于我 - 在使用UIManager注册ui类时,它需要完全限定的类名来实例化委托:

// here the ButtonCursorUI is in package
// org.jdesktop.swingx.plaf
defaults.add("ButtonUI", "org.jdesktop.swingx.plaf.ButtonCursorAddon$WindowsButtonCursorUI");
// in another package that would be
defaults.add("ButtonUI", myPackageName + ".ButtonCursorAddon$WindowsButtonCursorUI");

通常情况下,你会在something.plaf的LAF特定子包中使用不同的委托(而不是在插件本身)但是,这是一个例子: - )

答案 1 :(得分:5)

基本上,你必须走容器和子容器......

但要小心,你会对什么是按钮感到惊讶

public static void setButtonCursor(JComponent component, Cursor cursor) {

    for (Component comp : component.getComponents()) {

        if (comp instanceof JButton) {

            comp.setCursor(cursor);

        } else if (comp instanceof JComponent) {

            setButtonCursor((JComponent)comp, cursor);

        }

    }

}

这有一个很好的副作用,即走进JComboBox s(以及其他组件)并更改其按钮的光标,所以要小心;)