МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ
Національний університет “Львівська політехніка”
/
Розрахункова графічна робота
З курсу «Об’єктно-орієнтоване програмування»
1. ЗАВДАННЯ ДО ЛАБОРАТОРНОЇ РОБОТИ
Розробити програму додаток калькулятор використовуючи засоби бібліотеку JavaFX. Ця програма додаток повинна містити такі операції: додавання, віднімання, множення, ділення, дорівнює, “backspace”. В меню повинні бути реалізовані такі пункти: Файл, Редагування, Вид, Довідка. В підменю Файл: відкрити, вихід(пункт вихід відокремити розділовою лінією). В підменю Редагування: копіювати, вставити, вирізати. В підменю Вид: звичайний, інженерний. В підменю Довідка: Про програму. Для кожного пункту меню реалізувати мнемонічні операції та для пунктів підменю оперативні кнопки. Реалізувати спливаючі підказки для кожної кнопки. При написанні коду програми використовувати виключні ситуації, а також основні методи прокоментувати англійською мовою.
Варіант 6
x^4
M+
MC
Cos
min(String x, String y)
Знайти значення типу float та зменшити на 31. Відсортувати колонку зі значеннями по зростанні
Текст програми
SimpleCalculatorPane.java
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
public class SimpleCalculatorPane extends CalculatorPane{
JFXPanel fxPanel = new JFXPanel();
public SimpleCalculatorPane(){
// Create a GridPane as the root layout node
GridPane window1 = new GridPane();
window1.setPadding(new Insets(5));
window1.setHgap(5);
window1.setVgap(5);
// Create a BorderPane for the calculator
BorderPane windowContent = new BorderPane();
// Create the display field and place it at the Top area of the window
displayField = new TextField();
displayField.setAlignment(Pos.BASELINE_RIGHT);
windowContent.setTop(displayField);
//set a background of window
String backgroundStyle = "-fx-background-color: lightgreen;"
+ "-fx-background-radius: 30%;"
+ "-fx-background-inset: 5px;";
windowContent.setStyle(backgroundStyle);
// Create the components
button0 = new Button("0");
button1 = new Button("1");
button2 = new Button("2");
button3 = new Button("3");
button4 = new Button("4");
button5 = new Button("5");
button6 = new Button("6");
button7 = new Button("7");
button8 = new Button("8");
button9 = new Button("9");
buttonPoint = new Button(".");
buttonEqual = new Button("=");
buttonPlus = new Button("+");
buttonMinus = new Button("-");
buttonMult = new Button("*");
buttonDiv = new Button("/");
buttonSQRT = new Button("sqrt");
buttonMOD = new Button("%");
buttonINV = new Button("1/x");
buttonOPP = new Button("+/-");
buttonLOG = new Button(">>3");
buttonCOS = new Button("Cos");
buttonMIN = new Button("Max");
buttonWInt = new Button("WInt");
buttonWFlt = new Button("WFlt");
buttonWDbl = new Button("WDbl");
buttonWChr= new Button("WChr");
buttonBackspace = new Button("Backspace");
buttonCE = new Button("CE");
buttonC = new Button("C");
buttonMC = new Button("MC");
buttonMR = new Button("MR");
buttonMS = new Button("MS");
buttonMPlus = new Button("M+");
// Set a large preferred size of the buttons, so that they fill the whole space
//in their container
button0.setPrefSize(800, 800);
button1.setPrefSize(800, 800);
button2.setPrefSize(800, 800);
button3.setPrefSize(800, 800);
button4.setPrefSize(800, 800);
button5.setPrefSize(800, 800);
button6.setPrefSize(800, 800);
button7.setPrefSize(800, 800);
button8.setPrefSize(800, 800);
button9.setPrefSize(800, 800);
buttonPoint.setPrefSize(800, 800);
buttonEqual.setPrefSize(800, 800);
buttonPlus.setPrefSize(80, 800);
buttonMinus.setPrefSize(80, 800);
buttonMult.setPrefSize(80, 800);
buttonDiv.setPrefSize(80, 800);
// Create a GridPane node to hold 12 buttons �
//10 numeric ones, period, and the equal sign
GridPane gridPane = new GridPane();
// Add buttons to the gridPane
gridPane.add(button0,0,0);
gridPane.add(button1,1,0);
gridPane.add(button2,2,0);
gridPane.add(button3,0,1);
gridPane.add(button4,1,1);
gridPane.add(button5,2,1);
gridPane.add(button6,0,2);
gridPane.add(button7,1,2);
gridPane.add(button8,2,2);
gridPane.add(button9,0,3);
gridPane.add(buttonPoint,1,3);
gridPane.add(buttonEqual,2,3);
// Create a VBox and add Plus, Minus, Mult, Div buttons to it
VBox operations = new VBox();
operations.getChildren().add(buttonPlus);
operations.getChildren().add(buttonMinus);
operations.getChildren().add(buttonMult);
operations.getChildren().add(buttonDiv);
// Place the gridPane with buttons at the Center of the window(BorderPane)
windowContent.setCenter(gridPane);
// Place the VBox with operation buttons (Plus, Minus, Div, Mult)
//at the Right area of the border pane
windowContent.setRight(operations);
window1.add(windowContent, 0, 1);
getChildren().add(window1);
}
}
EngineerCalculatorPane.java
import javafx.embed.swing.JFXPanel;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
public class EngineerCalculatorPane extends CalculatorPane{
JFXPanel fxPanel = new JFXPanel();
private final double BUTTON_PREF_WIDTH = 800;
private final double BUTTON_PREF_HEIGHT = 800;
{
buttonWInt = new Button("WInt");
buttonWFlt = new Button("WFlt");
buttonWDbl = new Button("WDbl");
buttonWChr= new Button("WChr");
}
public EngineerCalculatorPane(){
super();
// Create a GridPane as the root layout node
GridPane windowContent = new GridPane();
windowContent.setPadding(new Insets(5));
windowContent.setHgap(5);
windowContent.setVgap(5);
buttonWInt.setStyle(""
+ "-fx-font-size: 12px;"
+ "-fx-font-weight: bold;");
buttonWFlt.setStyle(""
+ "-fx-font-size: 12px;"
+ "-fx-font-weight: bold;");
buttonWDbl.setStyle(""
+ "-fx-font-size: 11px;"
+ "-fx-font-weight: bold;");
buttonWChr.setStyle(""
+ "-fx-font-size: 11px;"
+ "-fx-font-weight: bold;");
// Create the display field and set the corresponding constraints
displayField = new TextField();
displayField.setAlignment(Pos.BASELINE_RIGHT);
GridPane.setColumnSpan(displayField, 7); // this cell is as wide as 6 other ones
GridPane.setRowSpan(displayField, 1); // this cell has the same height as other cells
GridPane.setFillHeight(displayField, true);
GridPane.setFillWidth(displayField, true); // fill all space in the cell
GridPane.setHalignment(displayField, HPos.CENTER);
GridPane.setValignment(displayField, VPos.CENTER); // position within the cell
GridPane.setHgrow(displayField, Priority.ALWAYS);
GridPane.setVgrow(displayField, Priority.ALWAYS); // grow larger than the preferred size if there is space
// Add the display field to the window
windowContent.add(displayField, 0, 1);
// Create the buttons
button0 = new Button("0");
button1 = new Button("1");
button2 =new Button("2");
button3 = new Button("3");
button4 = new Button("4");
button5 = new Button("5");
button6 = new Button("6");
button7 = new Button("7");
button8 = new Button("8");
button9 = new Button("9");
buttonPoint = new Button(".");
buttonEqual = new Button("=");
buttonPlus = new Button("+");
buttonMinus = new Button("-");
buttonMult = new Button("*");
buttonDiv = new Button("/");
buttonSQRT = new Button("sqrt");
buttonMOD = new Button("%");
buttonINV = new Button("1/x");
buttonOPP = new Button("+/-");
buttonLOG = new Button("x^4");
buttonCOS = new Button("Cos");
buttonMIN = new Button("Min");
buttonBackspace = new Button("Backspace");
buttonCE = new Button("CE");
buttonC = new Button("C");
buttonMC = new Button("MC");
buttonMR = new Button("MR");
buttonMS = new Button("MS");
buttonMPlus = new Button("M+");
// Set a large preferred size of the buttons,
// so that they fill the whole space
// in their container
button0.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button1.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button2.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button3.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button4.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button5.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button6.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button7.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button8.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
button9.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonPoint.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonEqual.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonPlus.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMinus.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMult.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonDiv.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMC.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMR.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMS.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMPlus.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonSQRT.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMOD.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonINV.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonOPP.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonLOG.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonCOS.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMIN.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonMS.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonWChr.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonWDbl.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonWFlt.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonWInt.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
buttonBackspace.setPrefSize(200, 800);
buttonCE.setPrefSize(200, 800);
buttonC.setPrefSize(200, 800);
// Add the buttons to the window
windowContent.add(button0,1,6);
windowContent.add(button1,1,5);
windowContent.add(button2,2,5);
windowContent.add(button3,3,5);
windowContent.add(button4,1,4);
windowContent.add(button5,2,4);
windowContent.add(button6,3,4);
windowContent.add(button7,1,3);
windowContent.add(button8,2,3);
windowContent.add(button9,3,3);
windowContent.add(buttonPoint,3,6);
windowContent.add(buttonSQRT,5,3);
windowContent.add(buttonMOD,5,4);
windowContent.add(buttonINV,5,5);
windowContent.add(buttonEqual,5,6);
windowContent.add(buttonOPP, 2, 6);
windowContent.add(buttonLOG, 6, 3);
windowContent.add(buttonCOS, 6, 4);
windowContent.add(buttonMIN, 6, 5);
windowContent.add(buttonWInt, 11, 3);
windowContent.add(buttonWFlt, 11, 4);
windowContent.add(buttonWDbl, 11, 5);
windowContent.add(buttonWChr, 11, 6);
GridPane.setRowIndex(buttonPlus, 6);
GridPane.setColumnIndex(buttonPlus, 4);
GridPane.setRowIndex(buttonMinus, 5);
GridPane.setColumnIndex(buttonMinus, 4);
GridPane.setRowIndex(buttonMult, 4);
GridPane.setColumnIndex(buttonMult, 4);
GridPane.setRowIndex(buttonDiv, 3);
GridPane.setColumnIndex(buttonDiv, 4);
windowContent.getChildren().addAll(buttonPlus, buttonMinus, buttonMult, buttonDiv);
// First column
GridPane.setRowIndex(buttonMC, 3);
GridPane.setColumnIndex(buttonMC, 0);
buttonMC.setPrefSize(BUTTON_PREF_WIDTH, BUTTON_PREF_HEIGHT);
GridPane.setRowIndex(buttonMR, 4);
GridPane.setColumnIndex(buttonMR, 0);
GridPane.setRowIndex(buttonMS, 5);
GridPane.setColumnIndex(buttonMS, 0);
GridPane.setRowIndex(buttonMPlus, 6);
GridPane.setColumnIndex(buttonMPlus, 0);
windowContent.getChildren().addAll(buttonMC, buttonMR, buttonMS, buttonMPlus);
// First row
GridPane.setHalignment(buttonBackspace, HPos.LEFT);
GridPane.setColumnSpan(buttonBackspace, 2);
GridPane.setFillHeight(buttonBackspace, true);
GridPane.setFillWidth(buttonBackspace, true); // fill all space in the cell
GridPane.setHgrow(buttonBackspace, Priority.ALWAYS);
GridPane.setVgrow(buttonBackspace, Priority.ALWAYS);
GridPane.setMargin(buttonBackspace, new Insets(2,40,2,2));
windowContent.add(buttonBackspace, 1, 2);
GridPane.setHalignment(buttonCE, HPos.CENTER);
GridPane.setColumnSpan(buttonCE, 3);
GridPane.setMargin(buttonCE, new Insets(2,40,2,40));
windowContent.add(buttonCE, 2, 2);
GridPane.setHalignment(buttonC, HPos.RIGHT);
GridPane.setColumnSpan(buttonC, 2);
GridPane.setMargin(buttonC, new Insets(2,2,2,40));
windowContent.add(buttonC, 4, 2);
getChildren().add(windowContent);
}
}
CalculatorPane.java
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public abstract class CalculatorPane extends StackPane {
private String firstNumber;
private String operator; // selected action
private double result;
private boolean memoryCellIsEmpty = true;
private double memoryCell = 0;
private String previouslyPressedButton = "";
protected Button buttonWInt;
protected Button buttonWFlt;
protected Button buttonWDbl;
protected Button buttonWChr;
private String secondNumber;
private int numberi;
private double numberd;
private float numberf;
private char numberc;
public Text memoryIndicator;
BufferedWriter writeTablein = null;
BufferedWriter writeTableout = null;
List<Integer> column1 = new ArrayList<Integer>(10); // Defining an integer Array List
List<Float> column2 = new ArrayList<Float>(10); // Defining an float Array List
List<Double> column3 = new ArrayList<Double>(10); // Defining an double Array List
List<Character> column4 = new ArrayList<Character>(10); // Defining an char Array List
public void handle(CalculatorEngine engine) {
button0.setOnAction((e) ->
engine.processEvent(e, this));
button1.setOnAction((e) ->
engine.processEvent(e, this));
button2.setOnAction((e) ->
engine.processEvent(e, this));
button3.setOnAction((e) ->
engine.processEvent(e, this));
button4.setOnAction((e) ->
engine.processEvent(e, this));
button5.setOnAction((e) ->
engine.processEvent(e, this));
button6.setOnAction((e) ->
engine.processEvent(e, this));
button7.setOnAction((e) ->
engine.processEvent(e, this));
button8.setOnAction((e) ->
engine.processEvent(e, this));
button9.setOnAction((e) ->
engine.processEvent(e, this));
buttonPoint.setOnAction((e) ->
engine.processEvent(e, this));
buttonEqual.setOnAction((e) ->
engine.processEvent(e, this));
buttonPlus.setOnAction((e) ->
engine.processEvent(e, this));
buttonMinus.setOnAction((e) ->
engine.processEvent(e, this));
buttonMult.setOnAction((e) ->
engine.processEvent(e, this));
buttonDiv.setOnAction((e) ->
engine.processEvent(e, this));
buttonSQRT.setOnAction((e) ->
engine.processEvent(e, this));
buttonMOD.setOnAction((e) ->
engine.processEvent(e, this));
buttonINV.setOnAction((e) ->
engine.processEvent(e, this));
buttonOPP.setOnAction((e) ->
engine.processEvent(e, this));
buttonLOG.setOnAction((e) ->
engine.processEvent(e, this));
buttonCOS.setOnAction((e) ->
engine.processEvent(e, this));
buttonMIN.setOnAction((e) ->
engine.processEvent(e, this));
buttonBackspace.setOnAction((e) ->
engine.processEvent(e, this));
buttonCE.setOnAction((e) ->
engine.processEvent(e, this));
buttonC.setOnAction((e) ->
engine.processEvent(e, this));
buttonMC.setOnAction((e) ->
engine.processEvent(e, this));
buttonMR.setOnAction((e) ->
engine.processEvent(e, this));
buttonMS.setOnAction((e) ->
engine.processEvent(e, this));
buttonMPlus.setOnAction((e) ->
engine.processEvent(e, this));
buttonWInt.setOnAction((e) ->
engine.processEvent(e, this));
buttonWFlt.setOnAction((e) ->
engine.processEvent(e, this));
buttonWDbl.setOnAction((e) ->
engine.processEvent(e, this));
buttonWChr.setOnAction((e) ->
engine.processEvent(e, this));
}
// Declare all calculator's components.
protected TextField displayField;
protected Button button0;
protected Button button1;
protected Button button2;
protected Button button3;
protected Button button4;
protected Button button5;
protected Button button6;
protected Button button7;
protected Button button8;
protected Button button9;
protected Button buttonPoint;
protected Button buttonEqual;
protected Button buttonPlus;
protected Button buttonMinus;
protected Button buttonMult;
protected Button buttonDiv;
protected Button buttonSQRT;
protected Button buttonMOD;
protected Button buttonINV;
protected Button buttonOPP;
protected Button buttonLOG;
protected Button buttonCOS;
protected Button buttonMIN;
protected Button buttonBackspace;
protected Button buttonCE;
protected Button buttonC;
protected Button buttonMC;
protected Button buttonMR;
protected Button buttonMS;
Button buttonMPlus;
// Constructor
public CalculatorPane() {
super();
String backgroundStyle = "-fx-background-color: grey;"
+ "-fx-background-inset: 10px;";
super.setStyle(backgroundStyle);
setStyle(backgroundStyle);
}
// Define getter and setters
public String getDisplayValue() {
return displayField.getText();
}
public void setDisplayValue(String val) {
String textColor = " -fx-text-fill: black";
displayField.setStyle(textColor);
displayField.setText(val);
}
public void setDisplayValue(String val, String color) {
String textColor = " -fx-text-fill: " + color;
displayField.setStyle(textColor);
displayField.setText(val);
}
public String getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(String firstNumber) {
this.firstNumber = firstNumber;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public double clearMemory() {
memoryCell = 0;
return memoryCell;
}
public double getMemory() {
return memoryCell;
}
public void setMemory(double memoryCell) {
this.memoryCell = memoryCell;
}
public void setResult(double result) {
this.result = result;
}
public String getPreviouslyPressedButton() {
return previouslyPressedButton;
}
public void setPreviouslyPressedButton(String previouslyPressedButton) {
this.previouslyPressedButton = previouslyPressedButton;
}
public void writeInt(String firstNumber) {
this.firstNumber = firstNumber;
numberi = Integer.parseInt(firstNumber);
try{
column1.add(numberi);}
catch (IndexOutOfBoundsException exception) {
setDisplayValue(String.valueOf(exception));}
}
public void writeFloat(String firstNumber) {
this.firstNumber = firstNumber;
//setting type of chars
numberf = Float.parseFloat(firstNumber);
column2.add(numberf);
}
public void writeDouble(String firstNumber) {
this.firstNumber = firstNumber;
//setting type of chars
numberd = Double.parseDouble(firstNumber);
column3.add(numberd);
}
public void writeChar(String firstNumber) {
this.firstNumber = firstNumber;
numberc = firstNumber.charAt(0);
column4.add(numberc);
}
public void write() {
try {
writeTablein = new BufferedWriter(new FileWriter("C:\\Users\\INTEL-Book\\Desktop\\in.txt"));
for (int i = 0; i < column1.size(); i++) {
writeTablein.write(column1.get(i) + "\t" + column2.get(i) + "\t" + column3.get(i) + "\t" + column4.get(i)); writeTablein.newLine(); } } catch (IOException exception) { System.err.println(exception); } catch (IndexOutOfBoundsException exception) { System.err.println(exception); } finally { if (writeTablein != null) { try { writeTablein.close(); } catch (IOException e) { System.err.println(e); } } } try{ for (int i = 0; i < column2.size(); i++) { column2.set(i, column2.get(i) - 31); } Collections.sort(column2); try { writeTableout = new BufferedWriter(new FileWriter("C:\\Users\\INTEL-Book\\Desktop\\out.txt")); for (int i = 0; i < column1.size(); i++) { writeTableout.write(column1.get(i) + "\t" + column2.get(i) + "\t" + column3.get(i) + "\t" + column4.get(i)); writeTableout.newLine(); } } catch (IOException exception) { System.err.println(exception); } } catch (IndexOutOfBoundsException exception) { System.err.println(exception); } finally { if (writeTableout != null) { try { writeTableout.close(); } catch (IOException e) { System.err.println(e); } } } } public void clearAll() { displayField.setText("0"); column1.clear(); column2.clear(); column3.clear(); column4.clear(); } protected void setButtonStyles(String style) { button0.setId(style); button1.setId(style); button2.setId(style); button3.setId(style); button4.setId(style); button5.setId(style); button6.setId(style); button7.setId(style); button8.setId(style); button9.setId(style); buttonPoint.setId(style); buttonEqual.setId(style); buttonPlus.setId(style); buttonMinus.setId(style); buttonMult.setId(style); buttonDiv.setId(style); buttonSQRT.setId(style); buttonMOD.setId(style); buttonINV.setId(style); buttonOPP.setId(style); buttonLOG.setId(style); buttonCOS.setId(style); buttonMIN.setId(style); buttonBackspace.setId(style); buttonCE.setId(style); buttonC.setId(style); buttonMC.setId(style); buttonMR.setId(style); buttonMS.setId(style); buttonMPlus.setId(style); }}
CalculatorEngine.java
import javafx.event.ActionEvent;@FunctionalInterfacepublic interface CalculatorEngine { void processEvent(ActionEvent e, CalculatorPane parent);}
Main.java
import javafx.application.Application;import javafx.beans.property.ReadOnlyDoubleProperty;import javafx.embed.swing.JFXPanel;import javafx.event.ActionEvent;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.input.KeyCode;import javafx.scene.input.KeyCodeCombination;import javafx.scene.input.KeyCombination;import javafx.scene.layout.GridPane;import javafx.scene.paint.Color;import javafx.stage.FileChooser;import javafx.stage.FileChooserBuilder;import javafx.stage.Stage;import javax.swing.*;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;public class Main extends Application { GridPane window; Stage primaryStage; final Stage link = primaryStage; TextArea fileTextArea = new TextArea(); private static SimpleCalculatorPane simpleCalculatorPane = new SimpleCalculatorPane(); private static EngineerCalculatorPane engineerCalculatorPane = new EngineerCalculatorPane(); public StringBuilder readFile(File selectedFile) { StringBuilder sb = new StringBuilder(1024); String curLine = ""; try { FileReader fr = new FileReader(selectedFile); BufferedReader br = new BufferedReader(fr); while (curLine != null) { curLine = br.readLine(); sb.append(curLine).append("\n"); } } catch (Exception e) { e.getMessage(); } return sb; } /////////MENU BAR////////// public MenuBar buildMenuBarWithMenus1(final ReadOnlyDoubleProperty menuWidthProperty) { final MenuBar menuBar1 = new MenuBar(); /////////FILE MENU////////// final Menu fileMenu = new Menu("_File"); fileMenu.setMnemonicParsing(true); final MenuItem openMenuItem = MenuItemBuilder.create() .text("Open") .onAction( (Event -> { String currentDir = System.getProperty("user.dir") + File.separator; StringBuilder sb = null; FileChooserBuilder fcb = FileChooserBuilder.create(); FileChooser fc = fcb.title("Open Dialog").initialDirectory(new File(currentDir)).build(); File selectedFile = fc.showOpenDialog(link); sb = readFile(selectedFile); fileTextArea.setText(sb.toString()); })) .accelerator( new KeyCodeCombination( KeyCode.O, KeyCombination.CONTROL_DOWN)) .build(); fileMenu.getItems().add(openMenuItem); fileMenu.getItems().add(new SeparatorMenuItem()); final MenuItem exitMenuItem = MenuItemBuilder.create() .text("Exit") .onAction( e -> System.exit(0)) .accelerator( new KeyCodeCombination( KeyCode.E, KeyCombination.CONTROL_DOWN)) .build(); fileMenu.getItems().add(exitMenuItem); menuBar1.getMenus().add(fileMenu); /////////EDIT MENU////////// final Menu editMenu = new Menu("_Edit"); editMenu.setMnemonicParsing(true); final MenuItem copyMenuItem = MenuItemBuilder.create() .text("Copy") .onAction( e -> System.out.println() ) .accelerator( new KeyCodeCombination( KeyCode.C, KeyCombination.CONTROL_DOWN)) .build(); editMenu.getItems().add(copyMenuItem); final MenuItem pasteMenuItem = MenuItemBuilder.create() .text("Paste") .onAction( e -> System.out.println()) .accelerator