简体   繁体   English

Java - BufferedImage没有出现在JFrame上

[英]Java - BufferedImage Not Appearing on JFrame

I'm having trouble getting an image to show on a JFrame. 我无法在JFrame上显示图像。 The frame is completely black upon running. 跑步时车架完全变黑。 Here's my code: 这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class JFrameTesting extends JFrame {

    BufferedImage test = null;

    public static void main(String[] args) throws URISyntaxException {
        new JFrameTesting();
    }
    public JFrameTesting() throws URISyntaxException {
        JFrame frame = new JFrame("My first JFrame!");
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
        } catch (IOException ex) {
            Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(test, 200, 200, null);
    }
}

I'm not sure if I'm nessecarily doing anything wrong. 我不确定我是不是自己做错了什么。 I have no errors at all when running. 跑步时我没有任何错误。

Thanks in advance! 提前致谢!

You haven't actually added your image to the JFrame yet. 您还没有实际将图像添加到JFrame。 To have the image appear you need to add the BufferedImage onto a component then draw that. 要显示图像,您需要将BufferedImage添加到组件上然后绘制它。 You can do that using a JLabel and an ImageIcon. 您可以使用JLabel和ImageIcon来实现。

public class JFrameTesting extends JFrame {

    BufferedImage test = null;
    ImageIcon image = new ImageIcon();

    public static void main(String[] args) throws URISyntaxException {
        new JFrameTesting();
    }
    public JFrameTesting() throws URISyntaxException {
        JFrame frame = new JFrame("My first JFrame!");
        try {
           test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
            image.setImage(test);
        } catch (IOException ex) {
            Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
        }
        JLabel label = new JLabel();
        label.setIcon(image);
        frame.add(label);
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

} }

Alternatively you can skip the Label and draw onto a component if you want. 或者,如果需要,您可以跳过标签并绘制到组件上。 In which case you you'll have to override the draw method of a JPanel. 在这种情况下,您将必须覆盖JPanel的draw方法。

JPanel pane = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 200, 200, null);
        }
    }; 
frame.add(pane);

Another note is that you're extending JFrame but also making a new JFrame inside of the class. 另一个注意事项是你正在扩展JFrame,但也在类中创建一个新的JFrame。 You can remove the extra JFrame and all the "frame." 您可以删除额外的JFrame和所有“框架”。 The class itself is a JFrame so you don't need an extra one. 该类本身是一个JFrame,因此您不需要额外的一个。

//set the title using the setTitle method
setTitle("My first JFrame!");

add(label);
setSize(400, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Also, I believe the ImageIO.read(...) method can take a URI as a parameter so you shouldn't have to create a File from it. 另外,我相信ImageIO.read(...)方法可以将URI作为参数,因此您不必从中创建文件。

My code draws image, but need repaint. 我的代码绘制图像,但需要重绘。 For this you need for example to change size of frame using you mouse. 为此,您需要使用鼠标更改框架的大小。

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class JFrameTesting extends JFrame {

    BufferedImage test = null;

    public static void main(String[] args) throws URISyntaxException {
        new JFrameTesting();
    }
    public JFrameTesting() throws URISyntaxException {
        JFrame frame = new JFrame("My first JFrame!");
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            System.out.println("init");
            test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
            System.out.println(test);
        } catch (IOException ex) {
            Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
        }

        final JPanel pane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                System.out.println("paint");
                super.paintComponent(g);
                g.drawImage(test, 0, 0, null);
            }
        };
        frame.add(pane);
        frame.repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        System.out.println("paint");
        g.drawImage(test, 200, 200, null);
    }
}

you can try, with this code. 您可以尝试使用此代码。 you need to load a JLabel on Jframe when you add a image. 添加图像时需要在Jframe上加载JLabel。

BufferedImage test = null;

public static void main(String[] args) throws URISyntaxException {
    new JFrameTesting();
}
public JFrameTesting() throws URISyntaxException {
    JFrame frame = new JFrame("My first JFrame!");
    JLabel label = new JLabel();
    frame.setSize(800, 800);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try {
        test = ImageIO.read(new File(getClass().getResource("test.png").toURI())); 
        frame.add( new JLabel(new ImageIcon(test)),BorderLayout.CENTER);
        frame.setIconImage(test);
        frame.setVisible(true);
        label.setVisible(true);
    } catch (IOException ex) {
        Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(test, 200, 200, null);
}

} }

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

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