通常使用Java Swing,您可以使用以下命令设置按钮的背景颜色:
myJButton.setBackground(Color.RED);
Run Code Online (Sandbox Code Playgroud)
这会导致按钮变红.但在Mac OS上,这种方法似乎被忽略了.该按钮只保留默认颜色.
如何在Mac OS上设置JButton的颜色?
cod*_*lhu 36
你试过设置JButton.setOpaque(true)吗?
JButton button = new JButton("test");
button.setBackground(Color.RED);
button.setOpaque(true);
Run Code Online (Sandbox Code Playgroud)
小智 33
你试过设置彩绘边框是假的吗?
JButton button = new JButton();
button.setBackground(Color.red);
button.setOpaque(true);
button.setBorderPainted(false);
Run Code Online (Sandbox Code Playgroud)
它适用于我的mac :)
小智 19
如果您不需要使用Apple的外观,一个简单的解决方法是在将任何GUI组件添加到JFrame或JApplet之前,将以下代码放在应用程序或applet中:
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这将为跨平台外观设置外观和感觉,然后setBackground()方法将用于更改JButton的背景颜色.