更改Swing应用程序上所有按钮的光标 [英] Change cursor for all buttons on Swing app

查看:214
本文介绍了更改Swing应用程序上所有按钮的光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Swing应用程序与主框架和一些其他形式加载其内。我需要实现一个一般的方法来设置所有按钮的手形光标在任何形式。

I have a Swing app with a main frame and some other forms loaded inside it. I Need to implement a general method to set the hand cursor for all buttons on any form.

这是类似于我们在css上的网页( input [type = button] {cursor:pointer;}

This is similar of what we do with css on web pages (input[type=button] { cursor:pointer; })

推荐答案

树像@Madprogrammer建议的是 方法,如果你想动态和/或在一个特定的形式更改光标。

Walking the tree like @Madprogrammer suggested is the method if you want to change the cursor dynamically and/or on a particular form.

只是为了好玩(和炫耀) SwingX 再次:) - 如果你想安装该游标全局,然后不在乎,安装一个ui委托,它照顾它。在SwingX中,它像实现和插入自定义按钮插件一样简单。副作用与其他答案相同(虽然不能解决)。通常的缺点(如同安装自定义ui代理时一样)是需要为所有LAF子类化和插件代理。

Just for fun (and to show-off SwingX again :) - if you want to install that cursor globally and then don't care about, install a ui-delegate which takes care of it. In SwingX, it's as simple as implementing and plugging a custom button addon. The side-effect is the same as in the other answer (though can't be solved as in that). The usual drawback (as always when installing custom ui delegates) is the need to subclass and plug-in delegates for all LAFs.

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类

I get this error: UIDefaults.getUI() failed: no ComponentUI class

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

Works for me - when registering the ui class with the UIManager, it needs the fully qualified class name to instantiate the delegate from:

// 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特定子包中有不同的委托(而不是addon本身)但是,这是一个例子: - )

Typically, you would have the different delegates in LAF specific subpackages of something.plaf (instead of in the addon itself) But then, it's an example :-)

这篇关于更改Swing应用程序上所有按钮的光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆