使用Windows LAF时,按钮的背景颜色是什么?

4

我有一个Java应用程序,它使用本地的LAF(Look and Feel)如下:

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

这很好运作,但是我想让一个按钮拥有红色背景色,结果却像这样: enter image description here 如你所见,我设置了按钮的背景和前景色,但结果并不美观。 有没有一种方法可以使按钮在不使用JButton子类化的情况下绘制红色背景?
4个回答

4
你必须了解,在Swing的外观结构下,JButton的UI代表负责绘制,而不是JButton本身,因此在这种情况下setBackground(...)将无法很好地工作。你最好是给按钮添加一个图标。

1
+1 给 图标; 更多建议在这里 - trashgod

4

我创建了自己的 CustomColorButton,具有漂亮的渐变色结束效果,并能够在 点击鼠标悬停 时正常运行。

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

您可以像这样创建新按钮:

CustomColorButton button = new CustomColorButton(Color.RED, Color.WHITE); // Background and font color
button.setText("Color button!");

The CustomColorButton class:

public class CustomColorButton extends JButton implements ActionListener, MouseListener
{
    private boolean hovered = false;
    private boolean clicked = false;

    private Color normalColor = null;
    private Color lightColor = null;
    private Color darkColor = null;

    public CustomColorButton(Color normalRedColor, Color fontColor)
    {
        setForeground(fontColor);

        this.normalColor = normalRedColor;
        this.lightColor = normalRedColor.brighter();
        this.darkColor = normalRedColor.darker();

        addActionListener(this);
        addMouseListener(this);
        setContentAreaFilled(false);
    }

    /**
     * Overpainting component, so it can have different colors
     */
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        GradientPaint gp = null;

        if (clicked)
            gp = new GradientPaint(0, 0, darkColor, 0, getHeight(), darkColor.darker());
        else if (hovered)
            gp = new GradientPaint(0, 0, lightColor, 0, getHeight(), lightColor.darker());
        else
            gp = new GradientPaint(0, 0, normalColor, 0, getHeight(), normalColor.darker());

        g2d.setPaint(gp);

        // Draws the rounded opaque panel with borders
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // For High quality
        g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 7, 7);

        g2d.setColor(darkColor.darker().darker());
        g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 7, 7);

        super.paintComponent(g);
    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        System.out.println("Button clicked!");
    }

    @Override
    public void mouseClicked(MouseEvent arg0)
    {

    }

    @Override
    public void mouseEntered(MouseEvent arg0)
    {
        hovered = true;
        clicked = false;

        repaint();
    }

    @Override
    public void mouseExited(MouseEvent arg0)
    {
        hovered = false;
        clicked = false;

        repaint();
    }

    @Override
    public void mousePressed(MouseEvent arg0)
    {
        hovered = true;
        clicked = true;

        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent arg0)
    {
        hovered = true;
        clicked = false;

        repaint();
    }
}

2

如果有人遇到和我一样的问题,这里是我采用的解决方法:

我使用ImageIcon将图像作为按钮的子项添加:

    BufferedImage stopPicture = null;
    try {
        stopPicture = ImageIO.read(new File("stop.png"));
    } catch (IOException ex) { }
    JLabel picLabel = new JLabel(new ImageIcon( stopPicture ));
    JButton btnStop = new JButton("");
    btnStop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            SerialTest.getInstance().stopMoving();
        }
    });
    btnStop.add(picLabel);

0
如果“不使用子类化”意味着无需自己扩展它,那么您可以选择使用SwingX JXButton,它扩展了JButton以使用绘图器(等等)。
JButton button = new JButton();
button.setBackground(bg);

变成

JXButton button = new JXButton();
button.setBackgroundPainter(new MattePainter(bg));

如果你必须坚持使用基本的JButton,我认为由于L&F的工作方式,没有解决方案。


自然而然地,我喜欢看到提到SwingX :-) 然而,你应该小心不要在几个问题中发布完全相同的答案 - 它们可能不适用于所有问题,或者这些问题是重复的,应该被标记/关闭。 - kleopatra
虽然两个答案都使用了相同的示例(因为它简单明了),但每个问题的答案都不同,即注意到这一个不需要被子类化(如果你注意到了,甚至可以看出另一个答案中的示例多了一行 ;))。事实上,当我尝试解决这个问题时,我发现了这两个问题,找到了这个(你的)解决方案,只是想分享一下。实际上,自从这些问题被提出以来,作者们可能已经解决了这个问题超过1年,只是为了帮助那些面临相同问题的人。 - Yago Méndez Vidal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接