无法将图像加载到JPanel中吗?

时间:2019-10-13 15:55:33

标签: java image swing

我是Java学习的新手,我被要求开发一个GUI应用程序来模拟四辆赛车,您可以设置每辆车的速度。以下是我的代码,它可以运行,但无法显示图片。对于将图片放置在什么位置(在JPanel或JFrame中),我不太了解。有人可以帮我完成这个项目吗? 预先感谢。

import JavaFX.scene.canvas.GraphicsContext;
import JavaFX.scene.image.ImageView;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Main {
    static class program extends JFrame {
         //create two JPanel objects.
        JPanel topRow = new JPanel();
        JPanel lowRow = new JPanel() {
            public void paint(Graphics graphics) {
              //draw a graph 
                graphics.drawRect(5, 25, 490, 240);
                graphics.drawLine(5, 85, 495, 85);
                graphics.drawLine(5, 145, 495, 145);
                graphics.drawLine(5, 205, 495, 205);
            }
        };
        private JTextField carped_a = new JTextField();
        String s1 = carped_a.getText();//there I want to change this to the //car's speed
        private JTextField carped_b = new JTextField();
        String s2 = carped_b.getText();
        private JTextField carped_c = new JTextField();
        String s3 = carped_c.getText();
        private JTextField carped_d = new JTextField();
        String s4 = carped_d.getText();
        program() {
            super("program");
            setLookAndFeel();
            setResizable(false);
            setSize(515, 330);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            BorderLayout layout = new BorderLayout();
            setLayout(layout);
            topRow.setBounds(5, 5, 490, 40);
            GridLayout grid = new GridLayout(1, 4, 20, 10);
            topRow.setLayout(grid);
            JLabel carLabel_a = new JLabel("car 1: ", JLabel.RIGHT);
            topRow.add(carLabel_a);
            carped_a.setEditable(true);
            topRow.add(carped_a);
            JLabel carLabel_b = new JLabel("car 2:", JLabel.RIGHT);
            topRow.add(carLabel_b);
            carped_b.setEditable(true);
            topRow.add(carped_b);
            JLabel carLabel_c = new JLabel("car 3:", JLabel.RIGHT);
            topRow.add(carLabel_c);
            carped_c.setEditable(true);
            topRow.add(carped_c);
            JLabel carLabel_d = new JLabel("car 4:", JLabel.RIGHT);
            topRow.add(carLabel_d);
            carped_d.setEditable(true);
            topRow.add(carped_d);
            add(topRow, BorderLayout.NORTH);
            add(lowRow, BorderLayout.CENTER);
        }

        private void setLookAndFeel() {
            try {
                UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (Exception exc) {
                // ignore error
            }
        }
    }
    static class Car extends JPanel{
        JLabel carLabel =new JLabel();
        Car(int a, int b) {
               carLabel.setBounds(a,b,50,40);
               this.add(carLabel);
               ImageIcon img=new ImageIcon("car.jpg");
               carLabel.setIcon(img);
               carLabel.setText(null);
        }
    }
    public static void main(String[] args) {
    program program1= new program();
    Car car1= new Car(5,45);
    }
}

我正在使用IntelliJ IDEA。

1 个答案:

答案 0 :(得分:1)

首先,您创建一个Car面板,但从未将其添加到框架中:

public static void main(String[] args) {
    program program1 = new program();
    Car car1 = new Car(5, 45);
    program1.add(car1);
}

其次,该图像未显示,因为它不在正确的路径中。当您说new JLabel("myimage.png");时,应该将图像放入项目的主目录中。但是,这样一来,您的应用程序将无法移植(实际上可以移植,但是您将不得不带走整个项目)。为了使其独立且可移植,请阅读this question.