How can I override the default mousepressed background color of all JButtons into a darker shade of its current background color?

huangapple go评论66阅读模式
英文:

How can I override the default mousepressed background color of all JButtons into a darker shade of its current background color?

问题

现在,它们都会获得默认的鼠标按下背景颜色,但如何将它设置为它们各自背景颜色的稍暗色调,而不是逐个设置呢?

我知道我可以在UIManager中覆盖颜色:

UIManager.put("Button.select", Color.RED);

我在这里找到了这个信息:https://stackoverflow.com/questions/14627223/how-to-change-a-jbutton-color-on-mouse-pressed

但这只会将它更改为单一颜色。按下任何按钮时,所有按钮都会变成红色背景。

有没有办法做到这样:

UIManager.put("Button.select", JButton.getBackground().darker());

我正在尝试学习Java Swing,所以请谅解我的无知。

英文:

Say I have ten JButtons each with their own background color explicitly set.
Now, they all get the default mousepressed background color but how can I set it to be a shade darker of their respective background colors without doing it one by one?

I know that I can override the color in UIManager :

UIManager.put("Button.select", Color.RED);

Which I found here <https://stackoverflow.com/questions/14627223/how-to-change-a-jbutton-color-on-mouse-pressed>

But that will change it to a single color only. All of my buttons will get a red background when pressed.

Is there a way to make it something like:

UIManager.put(&quot;Button.select&quot;, JButton.getBackground().darker());

I'm trying to learn java swing so bear with my ignorance.

答案1

得分: 1

以下是翻译好的内容:

创建自己的ButtonUI的一种方法是继承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,像这样:

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);
    }
}

唯一的问题是,所有的按钮都会一直看起来像金属外观。

英文:

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.

For example BasicButtonUI like so:

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 &lt; rows * cols; ++i) {
final JButton b = new JButton(&quot;Button&quot;);
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(&quot;App&quot;);
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);
}
}

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 &lt; rows * cols; ++i) {
final JButton b = new JButton(&quot;Button&quot;);
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(&quot;App&quot;);
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);
}
}

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

huangapple
  • 本文由 发表于 2020年8月8日 18:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63314374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定