package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends Application {
Controller sf = new Controller();
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Калькулятор");
primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("3185704.png")));
primaryStage.setScene(new Scene(root, 313, 445));
primaryStage.setResizable(false);
primaryStage.show();
primaryStage.getScene().setOnKeyPressed(e->{sf.sd(e.getCode());});
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.scene.input.KeyCode;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
public class Controller {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Button btn0;
@FXML
private Button btn1;
@FXML
private Button btn2;
@FXML
private Button btn3;
@FXML
private Button btn4;
@FXML
private Button btn5;
@FXML
private Button btn6;
@FXML
private Button btn7;
@FXML
private Button btn8;
@FXML
private Button btn9;
@FXML
private Button btndot;
@FXML
private Button btnclear;
@FXML
private Button btnDROP;
@FXML
private Button btnX;
@FXML
private Button btnplus;
@FXML
private Button btnminus;
@FXML
private Button btnback;
@FXML
private Button btnequals;
@FXML
private Text Result = new Text("");
private double d;
private String[] wer = {""};
private int[] sum = {0,0};
public void sd(KeyCode q){
switch (q){
case NUMPAD0: number("0"); break;
case NUMPAD1: number("1"); break;
case NUMPAD2: break;
case NUMPAD3: break;
case NUMPAD4: break;
case NUMPAD5: break;
case NUMPAD6: break;
case NUMPAD7: break;
case NUMPAD8: break;
case NUMPAD9: break;
case ADD: plus(); break;
default: System.out.println("NOT FOUND: "+q);
}
}
public void number (String n) {
if (Result.getText().length() < 14) {
wer[0] = wer[0] + n;
Result.setText(wer[0]);
}
}
public void plus () {
if (sum[0] == 2) {
X();
sum[0] = 1;
}
if (sum[0] == 3){
minus();
sum[0] = 1;
}
if (sum[0]==4) {
Drop();
sum[0] = 1;
} else {
if (wer[0].equals(""))
d=d+0;
else
d = d + Double.parseDouble(wer[0]);
if (String.valueOf(d).length()>=14)
Result.setText(String.valueOf(d).substring(0,14));
else
Result.setText(String.valueOf(d));
wer[0] = "";
sum[0] = 1;
}
}
public void minus () {
if (sum[0]==1) {
plus();
sum[0] = 3;
}
if (sum[0]==2) {
X();
sum[0] = 3;
}
if (sum[0]==4) {
Drop();
sum[0] = 3;
}else {
if (wer[0].equals(""))
d=d-0;
else
d = d - Double.parseDouble(wer[0]);
if (String.valueOf(d).length()>=14)
Result.setText(String.valueOf(d).substring(0,14));
else
Result.setText(String.valueOf(d));
wer[0] = "";
sum[0] = 3;
}
}
public void X () {
if (sum[0] == 1){
plus();
sum[0] = 2;
}
if (sum[0]==3) {
minus();
sum[0] = 2;
}
if (sum[0]==4) {
Drop();
sum[0] = 2;
} else {
if (!wer[0].equals(""))
d = d * Double.parseDouble(wer[0]);
// d = d * 1;
if (d==0)
d = 1 * Double.parseDouble(wer[0]);
else
d = d * 1;
// d = d * Integer.parseInt(wer[0]);
if (String.valueOf(d).length()>=14)
Result.setText(String.valueOf(d).substring(0,14));
else
Result.setText(String.valueOf(d));
wer[0] = "";
sum[0] = 2;
}
}
public void Drop() {
if (sum[0]==1) {
plus();
sum[0] = 4;
}
if (sum[0]==2) {
X();
sum[0] = 4;
}
if (sum[0]==3) {
minus();
sum[0] = 4;
}else {
if (!wer[0].equals(""))
d = d / Double.parseDouble(wer[0]);
// d = d / 1;
if (d==0)
d = Double.parseDouble(wer[0]);
else
d = d / 1;
// d = d / Integer.parseInt(wer[0]);
if (String.valueOf(d).length()>=14)
Result.setText(String.valueOf(d).substring(0,14));
else
Result.setText(String.valueOf(d));
wer[0] = "";
sum[0] = 4;
}
}
public void equals () {
if (sum[0]==1) {
plus();
sum[0] = 0;
}
if (sum[0]==2) {
X();
sum[0] = 0;
}
if (sum[0]==3) {
minus();
sum[0] = 0;
}
if (sum[0]==4) {
Drop();
sum[0] = 0;
}
}
@FXML
void initialize() {
//sum[0] = Integer.parseInt(wer[0]); из текста в цифры
//wer[0]=String.valueOf(sum[2]); из цифры в текст
btnX.setOnAction(actionEvent -> {
X();
});
btnplus.setOnAction(actionEvent -> {
plus();
});
btnminus.setOnAction(actionEvent -> {
minus();
});
btnDROP.setOnAction(actionEvent -> {
Drop();
});
btnclear.setOnAction(actionEvent -> {
Result.setText("0");
wer[0]="";
d=0;
sum[0]=0;
});
btnback.setOnAction(actionEvent -> {
wer[0]=Result.getText().substring(0,Result.getText().length()-1);
Result.setText(wer[0]);
});
btnequals.setOnAction(actionEvent -> {
equals();
});
btndot.setOnAction(actionEvent -> {
double k;
k = Double.parseDouble(Result.getText());
wer[0]=String.valueOf(k).substring(0,String.valueOf(k).length()-1);
Result.setText(wer[0]);
});
btn1.setOnAction(actionEvent -> {
number("1");
});
btn0.setOnAction(actionEvent -> {
number("0");
});
btn2.setOnAction(actionEvent -> {
number("2");
});
btn3.setOnAction(actionEvent -> {
number("3");
});
btn4.setOnAction(actionEvent -> {
number("4");
});
btn5.setOnAction(actionEvent -> {
number("5");
});
btn6.setOnAction(actionEvent -> {
number("6");
});
btn7.setOnAction(actionEvent -> {
number("7");
});
btn8.setOnAction(actionEvent -> {
number("8");
});
btn9.setOnAction(actionEvent -> {
number("9");
});
}
}