0

JPanel に画像を追加できない理由がわかりません。コードを使用します。

class PanelGlowny extends JPanel {

    private JLabel adam;
    private JButton henryk;

    PanelGlowny(){

        this.setLayout(new BorderLayout());
        ImageIcon imageurl = new ImageIcon("logo.jpg");
        //Image img = imageurl.getImage();
        adam = new JLabel(imageurl);
        add(adam, BorderLayout.NORTH);
        henryk = new JButton();
        add(henryk, BorderLayout.CENTER);

    }
}

画像はクラスと同じフォルダーにありますが、url を使用して画像を作成しても何も追加されません。このコードはボタンを追加しますが、画像は追加しません:(

コードは問題ないはずなので、問題はおそらく私のJDE、サンドボックス、またはこのようなsthにあります。

4

1 に答える 1

1

これを試して:

imageurl = new ImageIcon(getClass().getResource("logo.jpg"));

アイコンチュートリアルの使用方法を確認してください。

編集:リモート画像の読み込み

それを試して、ウェブから画像をロードしてください:

public static void main(String args[]) {
    try {
        JOptionPane.showMessageDialog(null, "", "", 
            JOptionPane.INFORMATION_MESSAGE, 
            new ImageIcon(new URL("http://marinerczarter.pl/wp-content/themes/twentyten/images/headers/path.jpg")));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    } 
}
于 2012-04-13T18:11:47.503 に答える