import java.awt.Color;
import javax.swing.JFrame;class SimpleFrame extends JFrame
{
public SimpleFrame()
{
setTitle( " GUI Frame " );
setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
setLocation( 50, 50 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible( true );

setBackground( Color.BLACK );
}
public final static  int DEFAULT_WIDTH = 200;
public final static  int DEFAULT_HEIGHT = 300;
}public class FrameTest
{
public static void main( String[] args )
{
new SimpleFrame();
}
}
为什么我的背景黑色显示不出来呢?

解决方案 »

  1.   

    import java.awt.Color;
    import javax.swing.JFrame;class SimpleFrame extends JFrame
    {
    public SimpleFrame()
    {
    setTitle( " GUI Frame " );
    setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
    setLocation( 50, 50 );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setVisible( true );

    setBackground( Color.BLACK );
    }
    public final static  int DEFAULT_WIDTH = 200;
    public final static  int DEFAULT_HEIGHT = 300;
    }public class FrameTest
    {
    public static void main( String[] args )
    {
    new SimpleFrame();
    }
    }重发次。
      

  2.   

    import java.awt.Color;
    import javax.swing.JFrame;class SimpleFrame extends JFrame
    {
    public SimpleFrame()
    {
    setTitle( " GUI Frame " );
    setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
    setLocation( 50, 50 );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setVisible( true );

    setBackground( Color.BLACK );
    }
    public final static  int DEFAULT_WIDTH = 200;
    public final static  int DEFAULT_HEIGHT = 300;
    }public class FrameTest
    {
    public static void main( String[] args )
    {
    new SimpleFrame();
    }
    }括号怎么都在没缩进。。
      

  3.   


    import java.awt.Color;
    import javax.swing.JFrame;class SimpleFrame extends JFrame
    {
    public SimpleFrame()
    {
    setTitle( " GUI Frame " );
    setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
    setLocation( 50, 50 );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setVisible( true );

    setBackground( Color.BLACK );
    //加入这一句
    this.getContentPane().setBackground(Color.BLACK);
    }
    public final static  int DEFAULT_WIDTH = 200;
    public final static  int DEFAULT_HEIGHT = 300;
    }public class FrameTest
    {
    public static void main( String[] args )
    {
    new SimpleFrame();
    }

      

  4.   

    加了不行。。我试过只能加上这句才行  this.getContentPane().setBackground(Color.BLACK);不知道为什么还有就是为什么我粘贴代码的时候不缩进呢?
      

  5.   


    import java.awt.BorderLayout;
    import java.awt.Color;
    //import java.awt.FlowLayout;import javax.swing.JFrame;
    import javax.swing.JPanel;@SuppressWarnings("serial")
    class SimpleFrame extends JFrame
    {
    public SimpleFrame()
    {
    setTitle( " GUI Frame " );
    setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
    setLocation( 300, 300 );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setVisible( true );
    //setLayout( new FlowLayout() );

    this.getContentPane().setBackground(Color.BLACK);
    }

    public final static  int DEFAULT_WIDTH = 200;
    public final static  int DEFAULT_HEIGHT = 300;
    }@SuppressWarnings("serial")
    class SimplePanel extends JPanel
    {
    public SimplePanel()
    {
    setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
    setLocation( 100, 50 );
    setBackground( Color.GREEN );
    }

    public final static  int DEFAULT_WIDTH = 300;
    public final static  int DEFAULT_HEIGHT = 100;
    }public class FrameTest
    {
    public static void main( String[] args )
    {
    SimpleFrame frame = new SimpleFrame();
    SimplePanel panel = new SimplePanel();
    SimplePanel panel2 = new SimplePanel();

    panel.setBackground( Color.GREEN );
    panel2.setBackground( Color.RED );

    frame.add( panel, BorderLayout.WEST );
    frame.add( panel2,  BorderLayout.SOUTH );
    }
    }
      

  6.   

    我帮你发哈:import java.awt.Color;
    import javax.swing.JFrame;class SimpleFrame extends JFrame {
    public SimpleFrame() {
    setTitle(" GUI Frame ");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    setLocation(50, 50);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true); setBackground(Color.BLACK);
    } public final static int DEFAULT_WIDTH = 200; public final static int DEFAULT_HEIGHT = 300;
    }public class FrameTest {
    public static void main(String[] args) {
    new SimpleFrame();
    }
    }
      

  7.   

    我说的就是这句        //加入这一句
            this.getContentPane().setBackground(Color.BLACK);
      

  8.   

    getContentPane().setBackground(Color.BLACK);
    可以解决问题。