javaプログラミングの初めの66歩目、電卓の計算部分の表示を行う〜Java資格、マイクラのプログラム理解〜

Опубликовано: 15 Июль 2026
на канале: Takunoji Java
101
0

JavaSwingのプログラミングです。このプログラミンでは、インスタンスがコンポーネント(画面部品)という形で目で見ることができます。
画面を作成していく中で、インスタンスの考え方がわかってくると思います。なので、基本として扱っています。

ポイントとしては、JLabelもJButtonもインスタンス化しているのでラベルとボタンがインスタンス化されています。
このプログラムでは、JFrame、JPanel、JLabel、 JButton x 20の23のインスタンスがあります。

【計算方法】
1. MtLabelのフィールド変数「view」に数値をセット
2. 同様にフィールド変数「ope」に演算子をセット
3. 再度数字を入力したとき→viewに値あり、かつ、opeに値ありのとき
 計算処理(calc())を行う

<テストクラスについて>
テストをするのに、おなじみ「JUnit」を使用します。JUnit4(org.junit)とJUnit5(org.junit.jupiter)でパッケージが違うので注意が必要です。4と5を混ぜないようにしましょう。テストの前処理は「BeforeAll」後処理は「AfterAll」のアノテーションをつければよいです。


【テストクラス】
public class MtLabelTest {
private static MtLabel target;

@BeforeAll
public static void init() {
target = new MtLabel("Test");
}
@AfterAll
public static void terminated() {
target = null;
}
@Test
public void testToNumber() {
assertEquals(1, target.toNumber("1"));
assertEquals(2, target.toNumber("2"));
assertEquals(3, target.toNumber("3"));
assertEquals(-1, target.toNumber("-1"));
}

@Test
public void testCalc() {
target.setText("1");
target.setOpe(MtButton.PLUS);
assertEquals(2, target.calc("1"));
target.setOpe(MtButton.MINUS);
assertEquals(0, target.calc("1"));
target.setOpe(MtButton.MULTIPLE);
assertEquals(1, target.calc("1"));
target.setOpe(MtButton.DIVIDE);
assertEquals(1, target.calc("1"));
}
}
【MtButtonクラスのコード】
public class MtButton extends JButton
implements ActionListener {
public static final String PLUS = "+";
public static final String MINUS = "-";
public static final String MULTIPLE = "*";
public static final String DIVIDE = "/";
private MtLabel lbl;
public MtButton(String mes) {
setText(mes);
setBackground(Color.RED);
addActionListener(this);
}
public MtButton(String mes, MtLabel lbl) {
this.lbl = lbl;
setText(mes);
//setBackground(Color.RED);
addActionListener(this);
}
public boolean isOpe(String ope) {
if (isEmpty(ope)) {
return false;
}
switch (ope) {
case PLUS:
System.out.println("PLUS");
break;
case MINUS:
System.out.println("MINUS");
break;
case MULTIPLE:
System.out.println("MULTIPLE");
break;
case DIVIDE:
System.out.println("DIVIDE");
break;
default:
System.out.println("想定外の値です。" + ope);
return false;
}
return true;
}
public boolean isNumber(String num) {
if (isEmpty(num)) {
return false;
}
return num.matches("[0-9]");
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello action");
String tmp = getText();
if (isNumber(tmp)) {
lbl.setText(tmp);
} else if (isOpe(tmp)) {
lbl.setOpe(tmp);
}
}
public boolean isEmpty(String st) {
if (st == null || "".equals(st)) {
return true;
}
return false;
}
}
【MtButtonクラスのコード】
public class MtLabel extends JLabel {
private String view;
private String ope;
public MtLabel(String mes) {
this.view = "";
this.ope = " ";
setText(this.view + this.ope);
setFont(new Font("Arial", Font.PLAIN, 24));
setHorizontalAlignment(JLabel.RIGHT);
setBorder(new LineBorder(Color.BLACK));
}

public int calc(String num) {
int left = toNumber(this.view);
int right = toNumber(num);
int ans = 0;
switch (this.ope) {
case MtButton.PLUS:
ans = left + right;
break;
case MtButton.MINUS:
ans = left - right;
break;
case MtButton.MULTIPLE:
ans = left * right;
break;
case MtButton.DIVIDE:
double d = left / right;
ans = (int) Math.round(d);
break;
default:
System.out.println("想定外の値: " + this.ope);
}
return ans;
}
public int toNumber(String num) {
return Integer.parseInt(num.trim());
}
public void setText(String mes) {
this.view += mes;
super.setText(this.view + this.ope);
}

public void setOpe(String ope) {
this.ope = ope;
setText("");
}
}