HelloButton2.java
Click here to get the file
サイズ
1.3 kB
-
File type
text/x-java
ファイルのコンテンツ
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);
}
}