簡體   English   中英

我無法顯示圖像

[英]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”。

有關加載資源的更多詳細信息,請查看此鏈接

如果該映像位於項目的根目錄中,則可以直接訪問它,類似於您所做的。 如果它在項目結構中的某個文件夾(例如資源文件夾)中,則需要使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM