用不同的渐变重新绘制摆动按钮

发布于 2024-10-08 01:52:42 字数 172 浏览 9 评论 0原文

单击 JButton 时如何用不同的渐变重新绘制它。我已经重写了paintComponent(Graphics)方法来进行初始绘制。 Onclick 我想重新绘制它,但我不希望用户在 actionperformed 事件中执行此操作,因为我希望这是一个独立的组件。

有什么想法可以实现这一点。

谢谢

How can I repaint a JButton with a different gradient when it is clicked. I have overridden the paintComponent(Graphics) method to do the initial paint. Onclick I want to repaint it but I dont want the user to be doing this in the actionperformed event as I want this to be a standalone component.

Any ideas how this can be achieved.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

前事休说 2024-10-15 01:52:42

最简单的方法是使用 setPressedIcon(),但您也可以覆盖 ButtonUI 委托,如此 示例

The easiest approach is to use setPressedIcon(), but you can also override paint() in the ButtonUI delegate, as shown in this example.

安静被遗忘 2024-10-15 01:52:42

还有另一个有趣的例子:

import java.util.List;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;

public class GradieltButton {

    public static void main(String[] args) {
        Object grad = UIManager.get("Button.gradient");
        List gradient;
        if (grad instanceof List) {
            gradient = (List) grad;
            System.out.println(gradient.get(0));
            System.out.println(gradient.get(1));
            System.out.println(gradient.get(2));
            System.out.println(gradient.get(3));
            System.out.println(gradient.get(4));
            //gradient.set(2, new ColorUIResource(Color.blue));
            //gradient.set(3, new ColorUIResource(Color.YELLOW));
            //gradient.set(4, new ColorUIResource(Color.GREEN));
            //gradient.set(2, new ColorUIResource(221, 232, 243));//origal Color
            //gradient.set(2, new ColorUIResource(255, 255, 255));//origal Color
            //gradient.set(2, new ColorUIResource(184, 207, 229));//origal Color
            gradient.set(2, new ColorUIResource(190, 230, 240));
            gradient.set(3, new ColorUIResource(240, 240, 240));
            gradient.set(4, new ColorUIResource(180, 200, 220));
            //UIManager.put("Button.background", Color.pink);
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GradieltButton().makeUI();
            }
        });
    }

    public void makeUI() {
        JButton button = new JButton("Click");
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

@ShaggyInjun 写了出于某种原因我的 UIManager.get("Button.gradient")
返回一个空值。你知道为什么吗?我知道我正在使用 MetalTheme。

UIManager 中的此键返回 ColorUIResource (UIManagerDefaults 中的更多内容,作者:@camickr

[0.3, 0.0, javax.swing.plaf.ColorUIResource[r=221,g=232,b=243],
javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],
javax.swing.plaf.ColorUIResource[r=184,g=207,b=229]]

需要使用 ColorUIResource 而不是 GradientButton.gradient 返回颜色和插图数组 == ColorUIResource

And another amusing example:

import java.util.List;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;

public class GradieltButton {

    public static void main(String[] args) {
        Object grad = UIManager.get("Button.gradient");
        List gradient;
        if (grad instanceof List) {
            gradient = (List) grad;
            System.out.println(gradient.get(0));
            System.out.println(gradient.get(1));
            System.out.println(gradient.get(2));
            System.out.println(gradient.get(3));
            System.out.println(gradient.get(4));
            //gradient.set(2, new ColorUIResource(Color.blue));
            //gradient.set(3, new ColorUIResource(Color.YELLOW));
            //gradient.set(4, new ColorUIResource(Color.GREEN));
            //gradient.set(2, new ColorUIResource(221, 232, 243));//origal Color
            //gradient.set(2, new ColorUIResource(255, 255, 255));//origal Color
            //gradient.set(2, new ColorUIResource(184, 207, 229));//origal Color
            gradient.set(2, new ColorUIResource(190, 230, 240));
            gradient.set(3, new ColorUIResource(240, 240, 240));
            gradient.set(4, new ColorUIResource(180, 200, 220));
            //UIManager.put("Button.background", Color.pink);
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GradieltButton().makeUI();
            }
        });
    }

    public void makeUI() {
        JButton button = new JButton("Click");
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

@ShaggyInjun wrote For some reason my UIManager.get("Button.gradient")
returns a null. Would you know why ? I know I am using MetalTheme.

this Key in UIManager returns ColorUIResource (more in UIManagerDefaults by @camickr)

[0.3, 0.0, javax.swing.plaf.ColorUIResource[r=221,g=232,b=243],
javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],
javax.swing.plaf.ColorUIResource[r=184,g=207,b=229]]

is required to use ColorUIResource instead of Gradient, Button.gradient returns arrays of Colors and Insets == ColorUIResource

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文