解説

swingで、画像を表示するサンプルです。

ポイント

  1. ImageIconクラスでアイコンとして画像を読み込む
  2. JLabelにアイコンを設定
大きさも変えずただ表示するだけならとっても簡単です。

実行結果


ソース ImageTest1.java

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);
	}
}
 

アフィリエイト

タグ:

Java swing 画像表示
最終更新:2013年03月09日 21:23