简体   繁体   English

从不调用paintComponent

[英]paintComponent is never called

I have been at this for a day now and I cannot understand why won't the paintComponent(Graphics g) method be called. 我已经待了一天,我不明白为什么不调用paintComponent(Graphics g)方法。 I use my second constructor to instantiate the objects, but still nothing. 我使用第二个构造函数来实例化对象,但仍然不执行任何操作。 My roommate has a VERY similar code and that works. 我的室友有一个非常相似的代码,并且可以正常工作。 What am I doing wrong? 我究竟做错了什么?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

public class squareImage extends JPanel{

Image picture;
String name;
baharKa outerPanel;    // a panel that I add these sqaures in
String topLabel;

int a = 0;
int b = 0;

public squareImage(baharKa out, String top, String name, Image im){  //This is the first constructor
    this.outerPanel = out;
    this.name = name;
    this.picture  = im;
    this.topLabel = top;
    this.setPreferredSize(new Dimension(80, 60));
    this.setBounds(a, b, getWidth(), getHeight());
    this.setPreferredSize(new Dimension(80,80));
    this.setOpaque(false);
}

public squareImage(baharKa out, String top, String name, Image im, int a, int b){  //This is second contructor
    this(out, top, name, im);
    this.a = a;
    this.b = b;
    this.setBounds(a, b, getWidth(), getHeight());
    out.revalidate();
}

protected void paintComponent(Graphics g) {     //This is never called
    super.paintComponent(g);
    System.out.println ("hi!");
    int i = g.getFontMetrics().stringWidth(name);
    int j = g.getFontMetrics().stringWidth(topLabel);
    g.drawImage(picture, 20, 20, 40, 40, null);
    g.setColor(Color.BLACK);
    g.setFont(new Font("Calibri", Font.PLAIN, 12));
    g.drawString(name, i/2 + 40, 45);
    g.drawString(name, j/2 + 40, 15);
}

} }

/////////I use it here/////// //////////我在这里使用它////////

protected void showMeTheSquares(){
        try {
            System.out.println("showme");
            wood = new squareImage(baharKaPanel, "0", "Wood", ImageIO.read(new File("images/wood.png")), 200, 75);
            metal = new squareImage(baharKaPanel, "0", "Metal", ImageIO.read(new File("images/metal.png")), 300, 75);
            plastic = new squareImage(baharKaPanel, "0", "Plastic", ImageIO.read(new File("images/plastic.png")), 400, 75);
            screwD = new squareImage(baharKaPanel, "0", "Plastic", ImageIO.read(new File("images/screwdriver.png")), 10, 100);
            hammer = new squareImage(baharKaPanel, "0", "Hammer", ImageIO.read(new File("images/hammer.png")), 10, 180);
            pliers = new squareImage(baharKaPanel,"0", "Pliers", ImageIO.read(new File("images/pliers.png")), 10, 340);
            scissors = new squareImage(baharKaPanel,"0", "Scissors", ImageIO.read(new File("images/scissors.png")), 10, 420);
            brush = new squareImage(baharKaPanel, "0", "Paint Brush", ImageIO.read(new File("images/paintbrush.png")), 10, 260);
            wood.repaint();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        addEverything();

    }

    protected void addEverything(){

        baharKaPanel.add(wood);
        baharKaPanel.add(metal);
        baharKaPanel.add(plastic);
        baharKaPanel.add(screwD);
        baharKaPanel.add(hammer);
        baharKaPanel.add(pliers);
        baharKaPanel.add(scissors);
        baharKaPanel.add(brush);
    }

This and your use of null layout is going to create a 0 by 0 sized component: 这和您对null布局的使用将创建一个大小为0 x 0的组件:

this.setBounds(a, b, getWidth(), getHeight());

If you put in code at this point and print out what getWidth() and getHeight() return in an unrendered component, you're going to find out that it's 0 or 1. 如果此时放入代码并打印出未渲染的组件中的getWidth()getHeight()返回,则将发现它是0或1。

You should not be using null layouts or setting bounds regardless, but rather using layout managers that respect preferred sizes, since the use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. 无论如何,您都不应使用空布局或设置边界,而应使用尊重首选大小的布局管理器,因为使用空布局会导致非常不灵活的GUI,尽管它们在一个平台上看起来不错,但在大多数其他平台上看起来却很糟糕或屏幕分辨率,并且很难更新和维护。

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

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