无法从 UIManager 获取 ButtonUI 来工作

问题描述 投票:0回答:1

我(和 ChatGPT 似乎)都没有设法正确使用 UIManager 的 ButtonUI 属性。这是我使用的默认包中的两个简单的类。只有我手动设置 CustomButtonUI 的 Button1 以橙色或灰色背景显示。由于我的 UIManager.put(... 语句,我希望 button2 具有相同的布局。代码不会抛出异常。

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class CustomButtonUIExample {

    public static void main(String[] args) {
        
        // UIManager-Registrierung
        UIManager.put("ButtonUI", CustomButtonUI.class.getName());

        JFrame frame = new JFrame("Custom Button UI Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        System.out.println(UIManager.get("ButtonUI"));
        JButton button1 = new JButton("Button 1");
        button1.setUI(new CustomButtonUI());
        JButton button2 = new JButton("Button 2");

        frame.add(button1);
        frame.add(button2);

        frame.pack();
        frame.setLocation(800, 500);
        frame.setVisible(true);
    }
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;

public class CustomButtonUI extends BasicButtonUI {
    @Override
    protected void installDefaults(AbstractButton b) {
        System.out.println("installDefaults called");
        super.installDefaults(b);
        b.setBackground(Color.gray);

        b.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                b.setBackground(Color.orange);
            }

            @Override
            public void focusLost(FocusEvent e) {
                b.setBackground(Color.gray);
            }
        });
    }
    
    @Override
    public void paint(Graphics g, JComponent c) {
        super.paint(g, c);
    }
}
java swing
1个回答
0
投票

您的

CustomButtonUI
类未声明
createUI
方法,因此它从 BasicButtonUI
 继承 
方法,该方法创建
BasicButtonUI
的实例。

因此,一个快速的解决方案是将这样的方法添加到您的

CustomButtonUI

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

但是只要您的 UI 实例不包含组件特定状态,它就可以是单例。这同样适用于焦点侦听器,它可以在不包含对组件的引用的情况下实现。然后,您可以通过简单的方式向 UI 实现添加正确的清理:

public class CustomButtonUI extends BasicButtonUI {
    static final CustomButtonUI INSTANCE = new CustomButtonUI();

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

    static final FocusListener FOCUS_LISTENER = new FocusListener() {
        @Override
        public void focusGained(FocusEvent e) {
            e.getComponent().setBackground(Color.ORANGE);
        }
  
        @Override
        public void focusLost(FocusEvent e) {
            e.getComponent().setBackground(Color.GRAY);
        }
    };

    @Override
    protected void installDefaults(AbstractButton b) {
        System.out.println("installDefaults called");
        super.installDefaults(b);
        b.setBackground(Color.GRAY);
        b.addFocusListener(FOCUS_LISTENER);
    }

    @Override
    protected void uninstallDefaults(AbstractButton b) {
        super.uninstallDefaults(b);
        b.removeFocusListener(FOCUS_LISTENER);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.