简体   繁体   English

setbackground在Jframe的构造函数中不起作用

[英]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. 我在玩JFrame,但是当我尝试设置JFrame的背景颜色时,它不起作用。 As far as I know you need to set the background color on the contentpane of the JFrame. 据我所知,您需要在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() 编辑:是的,我确实重写了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. 不要重写JFrame的paint方法。 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. 这就是为什么您当前的代码无法正常工作的原因,因为您不允许JFrame的超级对象进行必要的绘制。

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. 最好重写JPanel的paintComponent,并确保在重写中调用super.paintComponent(...)方法。 More important -- read the Java Swing graphics tutorials. 更重要的是-阅读Java Swing图形教程。

  • Custom drawing is done in paint() , for Swing JComponents paintComponent() instead of public drawCircles(int [][]circleArray){ 自定义绘制是在paint()中为Swing JComponents paintComponent()而不是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) 不要直接绘制到Top-Level Containers ,通过覆盖paintComponent()getPreferredSize(otherwise is there zero dimension)放置在JPanel

  • basic is very good described in Oracle tutorial 2D Graphics Oracle教程2D图形中对基本的描述非常好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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