javaプログラミングの初めの65歩目、String型をINT型に変換する〜Java資格、マイクラのプログラム理解〜

Опубликовано: 17 Август 2026
на канале: Takunoji Java
128
0

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

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

【処理内容】
1. Main#メインメソッド()を実行する
2. Sample#exev\cute()を実行する
3. JFrameのサイズと表示位置を設定する
4. 画面を終了したら、アプリケーションを終了する設定をする
5. JJFrameクラスから、Container クラスを取得する。
6. JLabelを作成する(インスタンス化)
7. JPanelをインスタンス化する
8. JPanelにBorderLayoutをセット
8. JLabelをJPanelの北にセットする
9. MtButtonをインスタンス化してJPanelの中央にセット
10. JButtonをインスタンス化してJPanelの南にセット
11. ContainerクラスにJPanelをセットする
12. JFrameを表示する(setVisible(true))

<テストクラスについて>
テストをするのに、おなじみ「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"));
}

}
【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;
}
}
【SampleClassクラスのコード】
public class SampleClass extends JFrame {

public void execute() {
System.out.println("Execute");
}
public void execute(String message) {
System.out.println(message);
setTitle(message);
setBounds(200, 200
, 300, 400);
setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
Container cont = getContentPane();
MtLabel lbl = new MtLabel(message);
JPanel pn = new JPanel();
pn.setLayout(new BorderLayout());
pn.add(lbl, BorderLayout.NORTH);
JPanel butPn = this.createButPanel();
for (int i = 0; i < 20; i++) {
butPn.add(new MtButton(
this.buttonNames()[i]
, lbl));
}
pn.add(butPn, BorderLayout.CENTER);
pn.add(new JButton("South"), BorderLayout.SOUTH);
cont.add(pn);
setVisible(true);
}
public JPanel createButPanel() {
JPanel butPn = new JPanel();
butPn.setLayout(new GridLayout(4, 5));
return butPn;
}
private String[] buttonNames() {
return new String[] {"AC", "7", "8", "9", "/"
, "%", "4", "5", "6", "*"
, "^", "1", "2", "3", "-"
, "√", "0", ".", "=", "+"};
}
}

【Mainクラスのコード】
public class Main {
public static void main(String[] args) {
SampleClass main = new SampleClass();
main.execute("Hello Goo");
}
}