将背景颜色设置为JButton [英] Setting background color to JButton

查看:247
本文介绍了将背景颜色设置为JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将背景颜色设置为 JButton 的问题。

I have a question about setting the background color to JButton.

更改边框的颜色。这里有区别(左侧是 jButton ):

It seems that the this method only changes the color of the border. Here is the difference (left is jButton):

有什么办法让背景一样吗?

Is there a way to make the background the same?

我在Windows 8上使用 setLookAndFeel

I'm using setLookAndFeel on Windows 8.

推荐答案

这将使用Metal(默认)或Windows PLAFs。

This will work with either the Metal (default) or Windows PLAFs.

import java.awt.*;
import javax.swing.*;

class ColoredButton {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                JButton b1 = new JButton("Button 1");
                b1.setBackground(Color.RED);
                // these next two lines do the magic..
                b1.setContentAreaFilled(false);
                b1.setOpaque(true);

                JOptionPane.showMessageDialog(null, b1);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于将背景颜色设置为JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆