javaの魂100まで-7

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class J {
public static void main(String[] arg) {
J7 frame = new J7();
frame.setVisible(true);
}
}

class J7 extends JFrame implements ActionListener {
JLabel label;
JButton[] button;//[] 配列の宣言
J7() {
setBounds(0,0,400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel();
button = new JButton[2];//[2] 要素数2の配列の作成
button[0] = new JButton("0");
button[1] = new JButton("1");
setLayout(new FlowLayout());
add(label);
add(button[0]);
add(button[1]);
button[0].addActionListener(this);
button[1].addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//if 判定、判断、比較、もし
if(e.getSource() == button[0]) {
label.setText("0");
}
if(e.getSource() == button[1]) {
label.setText("1");
}
}
}
/*

演習問題

"2"のボタンを追加し、
"2"のボタンをクリックすると、
ラベルに2が表示されるようにしてください。

*/
javaの魂100まで-6

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class J {
public static void main(String[] arg) {
J6 frame = new J6();
frame.setVisible(true);
}
}
//implements インターフェースの実装
class J6 extends JFrame implements ActionListener {
JLabel label;
JButton button;
J6() {
setBounds(0,0,400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel();
button = new JButton("1");
setLayout(new FlowLayout());
add(label);
add(button);
//addActionListener アクションイベント受渡の設定
button.addActionListener(this);
}
//actionPerformed アクションイベント受取のメソッド
public void actionPerformed(ActionEvent e) {
label.setText("1");
}
}
/*

演習問題

ボタンをクリックしたときに、
ボタンに表示されている文字が2になるようにしてください。

*/
javaの魂100まで-5

import javax.swing.*;
import java.awt.*;
class J {
public static void main(String[] arg) {
J5 frame = new J5();
frame.setVisible(true);
}
}

class J5 extends JFrame {
JLabel label;
JButton button;
J5() {
setBounds(0,0,400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel("java100");
button = new JButton("OK");
setLayout(new FlowLayout());//setLayout レイアウトを設定する
add(label);
add(button);
}
}
/*

演習問題

ボタンに表示されている文字列を、Goodに変更してください。

*/