简体   繁体   English

如何在Java中将图像添加到JPanel

[英]How to add an image to JPanel in java

I am using eclipse and search around for tutorials but none of them helped me much because I would get errors if I tried those functions. 我正在使用eclipse并在各处搜索教程,但是它们都对我没有多大帮助,因为如果尝试这些功能,我会出错。 I just wanna know what I am doing wrong or what should I use to get the code working. 我只是想知道我做错了什么或应该使用什么来使代码正常工作。 I Picture below is the final product, I just wanna add a picture and work towards adding text next. 我下面的图片是最终产品,我只想添加一张图片,然后继续添加文字。 So for now All i want to know is adding an image using GUI features. 所以现在我只想知道使用GUI功能添加图像。 Also, I tried the code below and doesn't show an image for some reason. 另外,我尝试了下面的代码,由于某种原因没有显示图像。 Also I would like to know what i am doing wrong and how i can correct it. 我也想知道我在做什么错以及如何纠正。 在此处输入图片说明

package Lab09Exercises;
import javax.swing.*;

public class Lab09Excercise extends JFrame {
JPanel jp = new JPanel();
JButton jb  = new JButton();


    public Lab09Excercise() {

        setTitle("Excercise2");
        setVisible(true);
        setSize(400,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        jb.setIcon(new ImageIcon("advertising.png"));
        jp.add(jb);
        add(jb);
        validate();
    }

    public static void main(String[] args) {


        Lab09Excercise a = new Lab09Excercise();
    }
}
[![the image is the result i want to get but i just wanana know how do add an image first before I can add rest of the components][1]][1]


  [1]: https://i.stack.imgur.com/QsvFp.png

Create your custom JPanel. 创建您的自定义JPanel。

Look, create a class called ImagePanel and add this code, then just replace JPanel with ImagePanel 看,创建一个名为ImagePanel的类并添加此代码,然后将JPanel替换为ImagePanel

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
       }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);           
    }

}

Answered by Brendan Cashman How to add an image to a JPanel? Brendan Cashman的回答如何将图像添加到JPanel?

Also

private void DisplayImage(JPanel jp, String url) {
    JLabel jl=new JLabel();
    jl.setIcon(new javax.swing.ImageIcon(getClass().getResource(url)));
    jp.add(jl);
}

Answered by FiroKun How can I display an image in a JPanel 由FiroKun解答如何在JPanel中显示图像

You can use JLabel to display images. 您可以使用JLabel显示图像。 It is probably more convenient to use because you can also display text with it and manipulate as any other component. 使用它可能更方便,因为您还可以显示文本并像其他任何组件一样进行操作。

ImageIcon imageIcon = new ImageIcon("path/to/image.jpg");
JLabel label = new JLabel("Sample text", imageIcon, JLabel.CENTER);

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

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