HelloButton.java
Click here to get the file
サイズ
1.1 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;
public class HelloButton {
public static void main(String[] args) {
// フレーム作成
final JFrame frame = new JFrame();
// 閉じるボタンの設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// フレームのサイズ
frame.setSize(200, 200);
// フレーム表示
frame.setVisible(true);
// 「Hello」ボタン作成
JButton button = new JButton("Hello");
// アクションリスナー追加
button.addActionListener(new ActionListener() {
// 「Hello」ボタンが押されると呼ばれる
public void actionPerformed(ActionEvent ae) {
// ラベル作成
JLabel label = new JLabel("Hello! World!!");
// フレームに「Hello! World」と表示
frame.getContentPane().add(label);
}
});
// フレームに「Hello」ボタンを追加
frame.add(button);
}
}