java button 背景色_java – 在Windows上设置JButton背景颜色

我有一个JButton,我想将背景设置为一种颜色.

JButton button = new JButton();

button.setVisible(true);

button.setPreferredSize(new Dimension(student_scroll.getWidth(), 50));

button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));

button.setBackground(Color.BLACK);

button.setForeground(Color.WHITE);

button.setOpaque(true);

我用这个用于mac,它出现了,因为我想要它.但是,在Windows上尝试它时,前景是白色的(应该如此),但背景是空的.

说添加button.setContentAreaFilled(false);我做了但没有效果.大多数人说要添加button.setOpaque(true);我也已经这样做了.

还有什么我必须做的,它会出现黑色背景?

编辑

根据要求,SSCCE:

import java.awt.Color;

import java.awt.Container;

import java.awt.Dimension;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class MainSwing extends JFrame {

private static final long serialVersionUID = -8231889836024827530L;

public static void main(String[] args) {

try {

System.setProperty("apple.laf.useScreenMenuBar", "true");

System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");

UIManager.put("ScrollBarUI", "main.CustomScrollBarUI");

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(ClassNotFoundException e) {

System.out.println("ClassNotFoundException: " + e.getMessage());

}

catch(InstantiationException e) {

System.out.println("InstantiationException: " + e.getMessage());

}

catch(IllegalAccessException e) {

System.out.println("IllegalAccessException: " + e.getMessage());

}

catch(UnsupportedLookAndFeelException e) {

System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());

}

SwingUtilities.invokeLater( new Runnable() {

public void run() {

JFrame frame = new JFrame() {

Container c = getContentPane();

JButton button = new JButton("Hello");

{

button.setText("Hello");

button.setVisible(true);

button.setPreferredSize(new Dimension(100, 50));

button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));

button.setBackground(Color.BLACK);

button.setForeground(Color.WHITE);

button.setOpaque(true);

c.add(button);

}

};

frame.setSize(500, 500);

frame.setBackground(Color.BLACK);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

});

}

}

似乎该问题与该行有关:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());当我删除它时,按钮是黑色的.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值