How to set the JFrame background color?

Setting JFrame background color is tricky one , we need to setBackground to contentPane of the JFrame not to the JFrame itself. We can pass Color Object to the setBackgound method to set background color of JFrame.


frame.getContentPane().setBackground(Color.blue);

Now lets see in the Example :


          JFrame frame = new JFrame("JFrame : Blue Colored ");
          
          frame.setPreferredSize(new Dimension(420,320));
          frame.pack();

          //Set the backgound color
          frame.getContentPane().setBackground(Color.blue);
          // here's the part where i center the jframe on screen
          frame.setLocationRelativeTo(null);
          
          frame.setVisible(true);
                                                                    


Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments: