How to Set the Background Color of a JButton on the Mac OS

前端 未结 5 1816
Happy的楠姐
Happy的楠姐 2020-11-30 08:55

Normally with Java Swing you can set the background color of a button with:

myJButton.setBackground(Color.RED);

which would cause the butto

相关标签:
5条回答
  • 2020-11-30 08:58

    Have you tried setting JButton.setOpaque(true)?

    JButton button = new JButton("test");
    button.setBackground(Color.RED);
    button.setOpaque(true);
    
    0 讨论(0)
  • 2020-11-30 08:59

    I own a mac too! here is the code that will work:

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

    before doing anything or adding any components set the look and feel so it looks better:

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

    That is Supposed to change the look and feel to the cross platform look and feel, hope i helped! :)

    0 讨论(0)
  • 2020-11-30 09:00

    Have you tried setting the painted border false?

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

    It works on my mac :)

    0 讨论(0)
  • 2020-11-30 09:08

    Based on your own purposes, you can do that based on setOpaque(true/false) and setBorderPainted(true/false); try and combine them to fit your purpose

    0 讨论(0)
  • 2020-11-30 09:23

    If you are not required to use Apple's look and feel, a simple fix is to put the following code in your application or applet, before you add any GUI components to your JFrame or JApplet:

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

    That will set the look and feel to the cross-platform look and feel, and the setBackground() method will then work to change a JButton's background color.

    0 讨论(0)
提交回复
热议问题