简体   繁体   中英

setbackground not working in constructor of Jframe

I'm playing with JFrames but when I try to set the background color of a JFrame it's not working. As far as I know you need to set the background color on the contentpane of the JFrame. in that case I don't see why this won't work.

class drawCircles extends JFrame{
int [][] s;
Container c;
public drawCircles(int [][]circleArray){
    super();
    setSize(400, 400);
    getContentPane().setBackground(Color.YELLOW);

    s=circleArray;
    show();
}

EDIT: yes I did override the paint()

public void paint (Graphics g){
    int width=this.getHeight()/10;
    int start=width;
    int endY=this.getHeight()-width;
    int endX=this.getWidth()-width;

    for(int i=0; i<s.length; i++){
        g.drawLine(i*width, start, i*width, endY);
        //g.drawLine(start, i*width, endX, i*width);

    }
    //g.drawRect(start, start, width*s.length,width*s.length);

    for(int i=0; i<s.length; i++){
        for(int j=0; j<s.length; j++){
            switch(s[i][j]){
            case 0: g.setColor(new Color(252, 177, 177));break;
            case 1: g.setColor(new Color(250, 165, 165));break;
            case 2: g.setColor(new Color(242, 156, 156));break;
            case 3: g.setColor(new Color(224, 133, 133));break;
            case 4: g.setColor(new Color(208, 117, 117));break;
            case 5: g.setColor(new Color(199, 107, 107));break;
            case 6: g.setColor(new Color(191, 98, 98));break;
            case 7: g.setColor(new Color(181, 88, 88));break;
            case 8: g.setColor(new Color(171, 79, 79));break;
            case 9: g.setColor(new Color(161, 71, 71));break;
            default:g.setColor(Color.white);

            }
            g.fillOval(j*width, i*width, width, width);
        }
    }

Don't override JFrame's paint method. Period. This is why your current code isn't working as you're not allowing the JFrame's super object to do its requisite painting.

While adding the super's method might well fix your problem:

public void paint (Graphics g) {
  super.paint(g);
  //... your code

Still you shouldn't do this since paint is responsible for many more things than painting a component including painting child objects and borders.

Better to override paintComponent of a JPanel and be sure to call the super.paintComponent(...) method in your override. More important -- read the Java Swing graphics tutorials.

  • Custom drawing is done in paint() , for Swing JComponents paintComponent() instead of public drawCircles(int [][]circleArray){

  • don't to draw directly to Top-Level Containers , put there JPanel by override paintComponent() and getPreferredSize(otherwise is there zero dimension)

  • basic is very good described in Oracle tutorial 2D Graphics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM