0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単な頭の体操計算

Last updated at Posted at 2017-05-04
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Kisan extends JFrame {

    public static void main(String[] args) {
        Game a = new Game();
        a.setVisible(true);
    }
}

class Game extends JFrame implements ActionListener {

    public JLabel mon;//問題の文字
    public JLabel sei2;//正解の文字
    public String saishosei = "これを解いてね";//最初の文字
    public int awase;//合わせた数
    public JTextField ko;//答えフィールドの答え
    public int sho, tugi;//足し算二つ
    public JLabel sei3;//正解のラベル

    Game() {
        /*基本設定*/
        super("計算機");
        this.setBounds(600, 500, 450, 300);//全体の位置
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//終了時の処理
        this.setVisible(true);//可視化
        /*テキスト作り*/
        /*下のボタン*/
        JPanel p = new JPanel();
        ko = new JTextField(30);//テキストの大きさ
        JButton tou = new JButton("どん");//ボタンの文字
        tou.addActionListener(this);//ボタンイベント受け
        ko.addActionListener(this);//テキストイベント受け
        p.add(ko);
        p.add(tou);
        Container contentPane = getContentPane();//全体を取得
        contentPane.add(p, BorderLayout.SOUTH);//ボタンの位置の調整
        /*上の問題作り*/
        Random rnd = new Random();//下4行ランダムな数字をだす
        Random rnd2 = new Random();
        sho = rnd.nextInt(100);
        tugi = rnd2.nextInt(100);
        awase = sho + tugi;
        /*中央の正解をだす*/
        JPanel sei = new JPanel();
        sei.setLayout(null);//まずパネルのレイヤーをnullにする
        sei3 = new JLabel(saishosei);//正解
        mon = new JLabel(sho + "+" + tugi);//問題の文字を記入
        sei3.setFont(new Font("MS ゴシック", Font.BOLD, 30));//正解の大きさを指定
        mon.setFont(new Font("Arial", Font.PLAIN, 30));//問題の大きさを指定
        sei3.setBounds(100, 130, 500, 35);//正解の場所指定
        mon.setBounds(170, 90, 600, 35);//問題の場所指定
        sei.add(sei3);
        sei.add(mon);
        contentPane.add(sei, BorderLayout.CENTER);//正解の位置
    }

    /*イベント受け取り用の関数*/
    public void actionPerformed(ActionEvent ev) {
        String ko2 = "";//初期化
        boolean frg = true;//フラグ
        //System.out.println(ko2);//確認用
        ko2 = ko.getText();//テキストから文字取得
        if (ko.getText().equals("") || ko2 == "") {
            sei3.setForeground(Color.BLACK);//色指定
            sei3.setBounds(56, 130, 500, 35);//正解の場所指定
            sei3.setText("文字入力をしてください");
            frg = false;
        }
        if (frg == true) {//if1
            awase = sho + tugi;//合体
            try {
                int ko4 = Integer.parseInt(ko2);//型変換
                if (ko4 == awase) {//if2
                    sei3.setText("正解です");
                    sei3.setForeground(Color.RED);//色指定
                    sei3.setBounds(148, 130, 500, 35);//正解の場所指定
                    ko.setText("");//文字を消す
                    Random rnd = new Random();//下4行ランダムな数字をだす
                    Random rnd2 = new Random();
                    sho = rnd.nextInt(100);
                    tugi = rnd2.nextInt(100);
                    mon.setText(sho + "+" + tugi);
                } else {
                    sei3.setText("はずれです");
                    sei3.setForeground(Color.BLUE);//色指定
                    sei3.setBounds(130, 130, 500, 35);//正解の場所指定
                    ko.setText("");//文字を消す
                }//if2終了
            }//try終了
            catch (NumberFormatException e) {
                sei3.setText("数字の文字を入れてください");//数値の文字以外が書かれたとき
                sei3.setForeground(Color.BLACK);//色指定
                sei3.setBounds(10, 130, 500, 35);//場所指定
                ko.setText("");//文字を消す
            }//catch終了
        }/*イベント受け取り関数終了*/
    }//if1終了
}//class終了

永遠と足し算をしていくだけのゲーム。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?