「Java/swing/サンプル/画像表示」の編集履歴(バックアップ)一覧に戻る
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
// swing 画像表示サンプル
// ポイント1.ImageIconクラスでアイコンとして画像を読み込む
// ポイント2.JLabelにアイコンを設定
// 大きさも変えずただ表示するだけならとっても簡単です。
public class ImageTest1 extends JFrame {
public static void main(String[] args) {
new ImageTest1();
}
public ImageTest1() {
setTitle("swing 画像表示サンプル1");
// ポイント1.ImageIconクラスでアイコンとして画像を読み込む
ImageIcon icon = new ImageIcon("logo3w.png");
// ポイント2.JLabelにアイコンを設定
JLabel l = new JLabel(icon);
add(l);
// ついでにJFrameのアイコンにも設定
setIconImage(icon.getImage());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100, 100, 300, 200);
setVisible(true);
}
}