Java JFrame background color not working

前端 未结 4 1461
名媛妹妹
名媛妹妹 2020-12-11 19:05

I tried using:

frame1.getContentPane().setBackground(Color.yellow);

But it is not working. Can anyone help me?

import java.         


        
4条回答
  •  遥遥无期
    2020-12-11 19:34

    Since you did not post an SSCCE, I will do it for you. This shows how to change the background color of a JFrame. Starting from this, you can start adding components to the JFrame and see where you go wrong, instead of letting us look at a few hundred lines of code.

    import javax.swing.JFrame;
    import java.awt.Color;
    import java.awt.EventQueue;
    
    public class ColoredFrame {
    
      public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
          @Override
          public void run() {
            JFrame frame = new JFrame( "TestFrame" );
            frame.getContentPane().setBackground( Color.PINK );
            //frame contains nothing, so set size
            frame.setSize( 200, 200 );
            frame.setVisible( true );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          }
        } );
      }
    }
    

提交回复
热议问题