简体   繁体   English

在JFrame中添加图像时输出错误

[英]Getting wrong output in adding an image in JFrame

My problem is that when I create or draw an image in JFrame by using 我的问题是,当我使用以下命令在JFrame中创建或绘制图像时

public void paint(Graphics g)

{} {}

Method I am getting a black screen instead of the image the problem code snippet is 方法我得到黑屏,而不是问题代码段的图像

ImageIcon i=new ImageIcon("logo.png);
Image im=i.getImage();
public void paint(Graphics g)
{
g=getGraphics();
}

Please suggest me an alternative method or solution to my problem Thanks in advance 请给我建议我的问题的替代方法或解决方案预先感谢

Would you consider using a JPanel and overriding the paintComponent method? 您是否考虑使用JPanel并覆盖paintComponent方法? Something like this: 像这样:

BufferedImage image = ... //i'll leave this blank since there are several ways to get a bufferedimage. I'll leave an eg: ImageIO.read(new File("/path/to/image"));
        JPanel pane = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }};

and then adding the panel to your frame. 然后将面板添加到框架中。 You can do the same with the container in the JFrame. 您可以对JFrame中的容器执行相同的操作。 The logic is simillar. 逻辑是相似的。

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

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