我打算把新建的窗口的背景设计为红色,怎么出来的窗口颜色没变还是默认的颜色
import java.awt.*;
import javax.swing.*;
public class primaryFrame extends JFrame{
public primaryFrame( ){
setTitle("welcome using");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
    public static final int DEFAULT_WIDTH=300;
    public static final int DEFAULT_HEIGHT=400;
 
public static void main(String[] args ){
primaryFrame myframe=new primaryFrame( );
myframe.setBackground(Color.RED);


}

解决方案 »

  1.   

    package action;import java.awt.*;import javax.swing.*;public class Frame1 extends JFrame {
        JPanel contentPane;
        BorderLayout borderLayout1 = new BorderLayout();    public Frame1() {
            try {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    /**
         * Component initialization.
         *
         * @throws java.lang.Exception
         */
        private void jbInit() throws Exception {
            contentPane = (JPanel) getContentPane();
            contentPane.setLayout(borderLayout1);
            setSize(new Dimension(400, 300));
            setTitle("Frame Title");
        }
        
        public void paint(Graphics g) {
            g.setColor(Color.red);
            g.fill3DRect(0,0,this.getWidth(),this.getHeight(),true);
        }
        
        public static void main(String[] args) {
            new Frame1().setVisible(true);
        }
    }
      

  2.   

    加这段:
    public void paint(Graphics g) { 
            g.setColor(Color.red); 
            g.fill3DRect(0,0,this.getWidth(),this.getHeight(),true); 
        }