简体   繁体   English

我不明白为什么图像不在JPanel中显示但在JFrame中正常显示的原因

[英]I can't understand the reason why Image is not displaying in JPanel but displaying normally in JFrame

I am creating a simple program in which I want to display an Image and a button.So I've created a JFrame . 我正在创建一个简单的程序,要在其中显示图像和按钮。因此,我创建了一个JFrame

This JFrame contains tow JPanel and one of the panels contains JButton and the other panel contains my image. 这个JFrame包含两个JPanel ,其中一个面板包含JButton ,另一个面板包含我的图像。 But image is not displaying in the panel. 但是图像未显示在面板中。 When I add image to the JFrame , it is displaying normally. 当我将图像添加到JFrame ,它正常显示。 Please help with the following code! 请帮助以下代码!

main() method: main()方法:

public class NewClass2 {
public static void main(String args[]){

    EventQueue.invokeLater(new Runnable(){
        public void run(){
            JFrame frm = new JFrm();
            frm.setVisible(true);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setSize(500,500);
        }
    });

}
}

JFrm class which initialize JFrame : 初始化JFrame JFrm类:

class JFrm extends JFrame{

JButton button;
JPanel panel0,panel1;

JFrm(){

    panel0 = new JPanel();
    panel1 = new JPanel();
    button = new JButton("Start");
    button.setPreferredSize(new Dimension(100, 30));
    panel0.add(button);

  panel1.add(new ImageComponent());   //adding image component to panel1 is not working
  //add(new ImageComponent());      //adding image component to the frame is working .. 


    add(panel1,BorderLayout.PAGE_START);
    add(panel0,BorderLayout.PAGE_END);

}

}

ImageComponent class which adds the image: ImageComponent类,用于添加图像:

class ImageComponent extends JComponent{
Image img;
ImageComponent(){
    img = new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png").getImage();

}


@Override
public void paint(Graphics g){

    g.drawImage(img, 100,100 , null);
}

}

How can I add image to JPanel ? 如何将图像添加到JPanel

Consider wrapping the image in a JLabel before adding it to the JPanel. 在将图像添加到JPanel之前,请考虑将其包装在JLabel中。 See Display a jpg image on a JPanel 请参见在JPanel上显示jpg图像

class JFrm extends JFrame{

JButton button;
JPanel panel0, panel1;

JFrm(){

    panel0 = new JPanel();
    panel1 = new JPanel();
    button = new JButton("Start");
    button.setPreferredSize(new Dimension(100, 30));
    panel0.add(button);

    ImageIcon image = new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png");
    panel1.add(new JLabel(image));

    add(panel1, BorderLayout.PAGE_START);
    add(panel0, BorderLayout.PAGE_END);

}
}

You did not set a size for your custom JComponent so it'll be squished by the LayoutManager to zero size. 您没有为自定义JComponent设置大小,因此LayoutManager会将其压缩为零大小。 Use setPreferredSize() to set whatever size seems suitable. 使用setPreferredSize()设置任何合适的大小。

ImageComponent(){
    ImageIcon temp = new ImageIcon("test.png");
    img = temp.getImage();
    setPreferredSize(new Dimension(temp.getIconWidth(),temp.getIconHeight()));
}


@Override
public void paint(Graphics g){
    g.drawImage(img, 0,0 , null);
}  

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

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