我无法显示图像

时间:2016-03-14 00:33:44

标签: java image swing icons jpanel

我正在处理一个聊天客户端程序,目前我可以在左侧获取文本窗格,文本输入。我可以添加按钮并将背景颜色更改为右边但我无法在右侧显示图像。从我读过的内容中有不止一种方法可以给猫上皮,但我正在尝试坚持我目前的设置,所以我不必重写所有内容。我理解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();
    }
}

提前致谢, -Brandon

2 个答案:

答案 0 :(得分:1)

您可以按照以下更改代码,它可以正常工作。刚刚在我自己的计算机上测试过它就可以了。

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

    label = new JLabel(icon);

您的问题似乎是您实际上没有加载图像。请记住使用ClassLoader加载资源文件。

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

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

答案 1 :(得分:0)

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

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

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);
相关问题