JavaRush /Java Blog /Random-TW /Java ImageIcon 和彎曲的手
DenisJNewb
等級 11

Java ImageIcon 和彎曲的手

在 Random-TW 群組發布
Java ImageIcon 和歪手 - 1
import javax.swing.*;
import java.awt.*;

public class TEST {
    public static void main(String[] args) {
        new Window();
    }

    static class Window extends JFrame {
        Window() {
            setSize(300, 250);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setLayout(new FlowLayout());

            ImageIcon icon = new ImageIcon("icon.jpg");

            setIconImage(icon.getImage());
            getContentPane().add(new JLabel(icon));

            setLocationRelativeTo(null);
            setVisible(true);
        }
    }
}
有這樣一段程式碼為什麼視窗中不顯示圖像以及為什麼不放置視窗圖示。icon.jpg 的大小為 32x32 像素,與其餘檔案一起位於 src 中(我使用 intellij idea)。執行程式碼的結果是一個帶有標準圖示的空視窗。
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION