且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何覆盖所有JButton的默认鼠标按下背景色,使其当前背景色更暗?

更新时间:2023-12-04 19:30:58

一种方法是创建自己的ButtonUI.为了避免重新发明***的麻烦,您可以扩展ButtonUI的子类.

One way to do this, is to create your own ButtonUI. To avoid the hassle of reinventing the wheel, you can exend a subclass of ButtonUI.

例如BasicButtonUI这样:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Objects;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicButtonUI;

public class MainWithBasicButtonUI {
    
    public static class SelectButtonUI extends BasicButtonUI {
        protected Color selectColor;
        
        public void setSelectColor(final Color selectColor) {
            this.selectColor = Objects.requireNonNull(selectColor);
        }
        
        public Color getSelectColor() {
            return selectColor;
        }
        
        @Override
        protected void paintButtonPressed(final Graphics g,
                                          final AbstractButton b){
            if (b.isContentAreaFilled()) {
                Dimension size = b.getSize();
                g.setColor(getSelectColor());
                g.fillRect(0, 0, size.width, size.height);
            }
        }
    }
    
    private static void createAndShowGUI() {
        final int rows = 3, cols = 3;
        final Color[] colors = new Color[]{Color.RED.brighter(), Color.GREEN.brighter(), Color.CYAN};
        final JPanel buttons = new JPanel(new GridLayout(rows, cols, 2, 2));
        final Random rand = new Random();
        for (int i = 0; i < rows * cols; ++i) {
            final JButton b = new JButton("Button");
            b.setBackground(colors[rand.nextInt(colors.length)]);
            final SelectButtonUI ui = new SelectButtonUI();
            ui.setSelectColor(b.getBackground().darker());
            b.setUI(ui);
            buttons.add(b);
        }
        final JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(buttons);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(MainWithBasicButtonUI::createAndShowGUI);
    }
}

,或者甚至更好的是,MetalButtonUI像这样:

or, even better, MetalButtonUI like so:

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Objects;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalButtonUI;

public class MainWithMetalButtonUI {
    
    public static class SelectButtonUI extends MetalButtonUI {
        public SelectButtonUI() {
            selectColor = super.getSelectColor();
        }
        
        public void setSelectColor(final Color selectColor) {
            this.selectColor = Objects.requireNonNull(selectColor);
        }
        
        @Override
        protected Color getSelectColor() {
            return selectColor;
        }
    }
    
    private static void createAndShowGUI() {
        final int rows = 3, cols = 3;
        final Color[] colors = new Color[]{Color.RED.brighter(), Color.GREEN.brighter(), Color.CYAN};
        final JPanel buttons = new JPanel(new GridLayout(rows, cols, 2, 2));
        final Random rand = new Random();
        for (int i = 0; i < rows * cols; ++i) {
            final JButton b = new JButton("Button");
            b.setBackground(colors[rand.nextInt(colors.length)]);
            final SelectButtonUI ui = new SelectButtonUI();
            ui.setSelectColor(b.getBackground().darker());
            b.setUI(ui);
            buttons.add(b);
        }
        final JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(buttons);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(MainWithMetalButtonUI::createAndShowGUI);
    }
}

唯一的问题是,您所有的按钮总是看起来像金属的L& F.

The only problem with this, is that all your buttons are always going to look like in the metal L&F.