如何在Mac OS上设置JButton的背景颜色

通常使用Java Swing,您可以使用以下命令设置按钮的背景颜色:

myJButton.setBackground(Color.RED); 

这会导致按钮变红。 但在Mac OS上,这种方法似乎被忽略了。 该按钮只保留默认颜色。

如何在Mac OS上设置JButton的颜色?

你试过设置JButton.setOpaque(true)吗?

 JButton button = new JButton("test"); button.setBackground(Color.RED); button.setOpaque(true); 

你试过设置彩绘边框是假的吗?

 JButton button = new JButton(); button.setBackground(Color.red); button.setOpaque(true); button.setBorderPainted(false); 

它适用于我的mac 🙂

如果您不需要使用Apple的外观,一个简单的解决方法是在将任何GUI组件添加到JFrame或JApplet之前,将以下代码放在应用程序或applet中:

  try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() ); } catch (Exception e) { e.printStackTrace(); } 

这将为跨平台外观设置外观和感觉,然后setBackground()方法将用于更改JButton的背景颜色。

我也拥有一台Mac! 这是可行的代码:

 myButton.setBackground(Color.RED); myButton.setOpaque(true); //Sets Button Opaque so it works 

在做任何事情或添加任何组件之前设置外观并使其看起来更好:

 try{ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }catch(Exception e){ e.printStackTrace(); } 

这是为了改变外观和感觉到跨平台的外观和感觉,希望我帮助! 🙂

根据您自己的目的,您可以基于setOpaque(true / false)和setBorderPainted(true / false)执行此操作; 尝试将它们结合起来以适合您的目的