私はJFrameに画像を表示する必要があるプロジェクトに取り組んでいます。イメージは動的にダウンロードされ、jar ファイルの外部に保存する必要があります。コンピュータのファイルシステムから (Jlabel を使用して) 画像を表示するにはどうすればよいですか?
2847 次
2 に答える
1
画像が「画像」などのフォルダーに保存されているとします。src (ソース フォルダー) 内に画像フォルダーがあると仮定していますが、任意の場所に変更できます。
/*
URL logoUrl = this.getClass().getResource("/images/login_icon.png"); // you can change this location
Toolkit tk1 = this.getToolkit();
logo = tk1.getImage(logoUrl);
*/
// use above code if the image lies within your jar file
// otherwise use below code for images stored in path like C:\User\Desktop ..
ImageIcon image = new ImageIcon("C:\\Users\\Public\\Pictures\\Desert.jpg");
jLabel2.setIcon(image); // where jLabel2 is your label
これはうまくいきます!
于 2013-05-31T09:32:18.273 に答える