我无法显示图像

I can't get image to display

我有一个正在开发的聊天客户端程序,目前我可以在左侧获得文本窗格和文本输入。我可以添加按钮并向右更改背景颜色,但无法在右侧显示图像。从我读过的内容来看,有不止一种方法可以在上面剥猫皮,但我试图坚持我目前拥有的设置,所以我不必重写所有内容。我了解 Java (OOP) 的基础知识及其工作原理。我只是迷失了如何格式化图像图标并显示该图像。这是代码:我正在使用 IntelliJ 进行编译。

package edu.lmu.cs.networking;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ChatClient {
    private BufferedReader in;
    private PrintWriter out;
    private JFrame frame = new JFrame("Chatter");
    private JTextField textField = new JTextField(20);
    private JTextArea messageArea = new JTextArea(8, 40);
    private JPanel panel;
    private JButton button;
    private JLabel label;

    public ChatClient() {
        textField.setEditable(false);
        messageArea.setEditable(false);
        // frame.setSize(500, 500);
        // frame.setVisible(true);
        frame.getContentPane().add(textField, "South");
        frame.getContentPane().add(new JScrollPane(messageArea), "West");
        panel = new JPanel();
        panel.setBackground(Color.YELLOW);
        button = new JButton("Button");
        label = new JLabel(new ImageIcon("x.gif"));
        panel.add(button);
        panel.add(label, BorderLayout.EAST);
        frame.add(panel);
        frame.pack();
        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.println(textField.getText());
                textField.setText("");
            }
        });
    }

    private String getServerAddress() {
        return JOptionPane.showInputDialog(frame, "Enter IP Address of the Server:",
                "Welcome to the Chatter", JOptionPane.QUESTION_MESSAGE);
    }

    private String getName() {
        return JOptionPane.showInputDialog(frame, "Choose a screen name:", "Screen name selection",
                JOptionPane.PLAIN_MESSAGE);
    }

    private void run() throws IOException {
        // Make connection and initialize streams
        String serverAddress = getServerAddress();
        Socket socket = new Socket(serverAddress, 5910);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
        while (true) {
            String line = in.readLine();
            if (line.startsWith("SUBMITNAME")) {
                out.println(getName());
            } else if (line.startsWith("NAMEACCEPTED")) {
                textField.setEditable(true);
            } else if (line.startsWith("MESSAGE")) {
                messageArea.append(line.substring(8) + "\n");
            }
        }
    }

    public static void main(String[] args) throws Exception {
        ChatClient client = new ChatClient();
        client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        client.frame.setVisible(true);
        client.run();
    }
}

提前致谢, -布兰登

您只需按以下方式更改代码即可。刚在我自己的电脑上测试过,成功了。

    ImageIcon icon = new ImageIcon(getClass().getResource("x.gif"));

    label = new JLabel(icon);

看来你的问题是你没有真正加载图片。记得使用ClassLoader来加载资源文件。

您应该将 'x.gif' 放在您的项目目录或您的资源文件夹(首选)下以使其工作。

有关加载资源的更多详细信息,请查看 this link

如果图片在你的项目根目录下,那么你可以直接访问它,类似于你所做的。 如果它位于项目结构中的某个文件夹(比如资源文件夹)中,则需要使用 getClass().getResource("/resources/x.gif").

您还可以创建图像的缩放版本,指定高度和 width.It 可以使用下面的示例代码完成:

ImageIcon icon = new ImageIcon("x.gif");
  Image img = icon.getImage();
  Image newimg = img.getScaledInstance(30, 20,
            java.awt.Image.SCALE_SMOOTH);
    icon = new ImageIcon(newimg);
  label = new JLabel(icon);