说我有十个JButtons,每个都有明确设置的自己的背景色. 现在,它们都具有默认的鼠标按下背景色,但是如何将它们设置为各自背景色的暗色,而又不一一对应呢?
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?
我知道我可以覆盖UIManager中的颜色:
I know that I can override the color in UIManager :
UIManager.put("Button.select", Color.RED);我在这里找到的如何在其上更改JButton颜色鼠标按下了吗?
但是这只会将其更改为单一颜色.按下时,我所有的按钮都会变成红色背景.
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("Button.select", JButton.getBackground().darker());我正在尝试学习Java摇摆,所以请忍着我的无知.
I'm trying to learn java swing so bear with my ignorance.
推荐答案一种方法是创建自己的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.
更多推荐
如何覆盖所有JButton的默认鼠标按下背景色,使其当前背景色更暗?
发布评论