実行結果

この電卓を作る時にjavafxを利用したのですが、
自分のパソコンにその環境がなく学校のPCでやったはいいものの、
学校のPCは画面収録の機能がなくスマホで撮影した動画を
gifにして投稿しました。
見にくくてすみません。
import java.io.*;
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.event.*;
public class Calculator extends Application
{
private TextField tf;
private Button[][] bt=new Button[4][5];
public static void main(String[] args)
{
public void start(Stage stage)throws Exception
{
tf=new TextField();
tf.setEditable(false);
tf.setMaxWidth(380);
tf.setFont(Font.font("MonoSpace",40));
tf.setAlignment(Pos.CENTER_RIGHT);
String[][] bt_str={{"CE","C","BS","/"},{"7","8","9","*"},{"4","5","6","-"},{"1","2","3","+"},{"±","0",".","="}};
for(int i=0; i<4; i++){
for(int j=0; j<5; j++){
bt[i][j]=new Button(bt_str[j][i]);
bt[i][j].setPrefWidth(95);
bt[i][j].setPrefHeight(95);
bt[i][j].setFont(Font.font("MonoSpace",30));
bt[i][j].setOnAction(new ButtonEventHandler());
}
}
GridPane gp=new GridPane();
gp.setHgap(2);
gp.setVgap(2);
for(int i=0; i<4; i++){
for(int j=0; j<5; j++){
gp.add(bt[i][j],i,j);
}
}
gp.setAlignment(Pos.CENTER);
BorderPane bp=new BorderPane();
bp.setTop(tf);
bp.setAlignment(tf,Pos.CENTER);
bp.setCenter(gp);
Scene sc=new Scene(bp,400,600);
stage.setScene(sc);
stage.setTitle("電卓");
stage.show();
}
class ButtonEventHandler implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
String in=((Button)e.getSource()).getText();
StringBuffer stb=new StringBuffer(tf.getText());
String str=new String(stb.toString());
String regex="[+\\-]?[0-9]+.?[0-9]*[+\\-\\*/]{1}[0-9]+.?[0-9]*";
if(in=="="){
if(str.matches(regex)){
String regex2="(?=[+\\-\\*/])";
String[] operands=str.toString().split(regex2);
if(operands[1].charAt(0)=='+'){
double op1=Double.parseDouble(operands[0]);
double op2=Double.parseDouble(operands[1].substring(1));
double result=op1+op2;
str=String.valueOf(result);
tf.setText(str);
}
else if(operands[1].charAt(0)=='-'){
double op1=Double.parseDouble(operands[0]);
double op2=Double.parseDouble(operands[1].substring(1));
double result=op1-op2;
str=String.valueOf(result);
tf.setText(str);
}
else if(operands[1].charAt(0)=='*'){
double op1=Double.parseDouble(operands[0]);
double op2=Double.parseDouble(operands[1].substring(1));
double result=op1*op2;
str=String.valueOf(result);
tf.setText(str);
}
else if(operands[1].charAt(0)=='/'){
double op1=Double.parseDouble(operands[0]);
double op2=Double.parseDouble(operands[1].substring(1));
double result=0.0;
result=op1/op2;
str=String.valueOf(result);
tf.setText(str);
}
}
}
else if(in=="BS"){
stb.deleteCharAt(stb.length()-1);
str=new String(stb.toString());
tf.setText(str);
}
else if(in=="C" || in=="CE"){
stb.delete(0,stb.length());
str=new String(stb.toString());
tf.setText(str);
}
else if(in=="±"){
if(stb.charAt(0)!='+' && stb.charAt(0)!='-' && stb.charAt(0)!='*' && stb.charAt(0)!='/' && stb.charAt(0)!='.'){
stb.insert(0,"-");
str=new String(stb.toString());
tf.setText(str);
}
}
else{
stb.append(in);
str=new String(stb.toString());
tf.setText(str);
}
}
}
}
コメント