Personal tools
Document Actions

HelloButton2.java

by fujisaki last modified 2008-12-08 02:15

Click here to get the file

Size 1.3 kB - File type text/x-java

File contents

package exercise.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class HelloButton2 {
    
    public static void main(String[] args) {
	// フレーム作成
	JFrame frame = new JFrame();

	// 閉じるボタン設定
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	// フレームのサイズ設定
	frame.setSize(200, 200);

	// フレーム表示
	frame.setVisible(true);
		
	// パネル作成
	final JPanel panel = new JPanel();

	// フレームにパネルを追加
	frame.getContentPane().add(panel);
	
	
	// 「Hello」ボタン作成
	JButton button = new JButton("Hello");
	
	// 「Hello」ボタンにアクションリスナ追加
	button.addActionListener(new ActionListener() {
		// 「Hello」ボタンが押されると呼ばれる
		public void actionPerformed(ActionEvent arg0) {
		    // ラベル作成
		    JLabel label = new JLabel("Hello! World!!");

		    // パネルに「Hello! World!!」ラベル追加
		    panel.add(label);

		    // パネル更新
		    panel.revalidate();
		}
	    }); 
	
	// パネルに「Hello」ボタン追加
	panel.add(button);
    }
}