在JavaFX中为计算器创建退格按钮
我正在尝试创建一个JavaFX计算器,这是一个非常基本的计算器,但我不确定如何实现退格在JavaFX中为计算器创建退格按钮,java,javafx,calculator,backspace,Java,Javafx,Calculator,Backspace,我正在尝试创建一个JavaFX计算器,这是一个非常基本的计算器,但我不确定如何实现退格 private void makeBackspaceButton(Button button) { button.setStyle("-fx-base: ivory;"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionE
private void makeBackspaceButton(Button button) {
button.setStyle("-fx-base: ivory;");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
value.set(value.get());
}
});
}
if (OutputTextField.getText().length()>0){
StringBuffer sb = new StringBuffer(OutputTextField.getText());
sb = sb.deleteCharAt(OutputTextField.getText().length()-1);
OutputTextField.setText(sb.toString());
}
}
private void makebackspace按钮(按钮){
button.setStyle(“-fx base:ivory;”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
value.set(value.get());
}
});
}
我一生都想不出一种方法来删除最后输入的值。因此,如果输入值89,则仅删除最后一位数字(9)。
有没有办法用这种格式,或者我需要改变我的格式。
我的完整代码如下,它还没有完成
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.util.HashMap;
import java.util.Map;
// a simple JavaFX calculator.
public class Calcy extends Application {
//implementing the template if how i would like the buttons to look like
private static final String[][] template = {
{"c", "%", "√", "←"},
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"}
};
private final Map<String, Button> accelerators = new HashMap<>();
private DoubleProperty stackValue = new SimpleDoubleProperty();
private DoubleProperty value = new SimpleDoubleProperty();
private enum Op {
NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE
}
private Op curOp = Op.NOOP;
private Op stackOp = Op.NOOP;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
final TextField screen = createScreen();
final TilePane buttons = createButtons();
stage.setTitle("Thank goodness for a calculator!");
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
stage.setScene(new Scene(createLayout(screen, buttons)));
stage.show();
}
private VBox createLayout(TextField screen, TilePane buttons) {
final VBox layout = new VBox(20);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: floralwhite; -fx-padding: 20; -fx-font-size: 20;");
layout.getChildren().setAll(screen, buttons);
handleAccelerators(layout);
screen.prefWidthProperty().bind(buttons.widthProperty());
return layout;
}
private void handleAccelerators(VBox layout) {
layout.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
Button activated = accelerators.get(keyEvent.getText());
if (activated != null) {
activated.fire();
}
}
});
}
private TextField createScreen() {
final TextField screen = new TextField();
screen.setStyle("-fx-background-color: lightgrey;");
screen.setAlignment(Pos.CENTER_RIGHT);
screen.setEditable(false);
screen.textProperty().bind(Bindings.format("%.2f", value));
return screen;
}
private TilePane createButtons() {
TilePane buttons = new TilePane();
buttons.setVgap(7);
buttons.setHgap(7);
buttons.setPrefColumns(template[0].length);
for (String[] r : template) {
for (String s : r) {
buttons.getChildren().add(createButton(s));
}
}
return buttons;
}
private Button createButton(final String s) {
Button button = makeStandardButton(s);
if (s.matches("[0-9]")) {
makeNumericButton(s, button);
} else {
final ObjectProperty<Op> triggerOp = determineOperand(s);
if (triggerOp.get() != Op.NOOP) {
makeOperandButton(button, triggerOp);
} else if ("c".equals(s)) {
makeClearButton(button);
} else if ("=".equals(s)) {
makeEqualsButton(button);
} else if ("←".equals(s)) {
makeBackspaceButton(button);
} else if ("%".equals(s)) {
makePercentageButton(button);
} else if ("√".equals(s)) {
makeSquarerootButton(button);
} else if (".".equals(s)) {
makePointButton(button);
}
}
return button;
}
private ObjectProperty<Op> determineOperand(String s) {
final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
switch (s) {
case "+":
triggerOp.set(Op.ADD);
break;
case "-":
triggerOp.set(Op.SUBTRACT);
break;
case "*":
triggerOp.set(Op.MULTIPLY);
break;
case "/":
triggerOp.set(Op.DIVIDE);
break;
case "%":
}
return triggerOp;
}
private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
button.setStyle("-fx-base: ivory;");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
curOp = triggerOp.get();
}
});
}
private Button makeStandardButton(String s) {
Button button = new Button(s);
button.setStyle("-fx-base: ivory;");
accelerators.put(s, button);
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
return button;
}
private void makeNumericButton(final String s, Button button) {
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if (curOp == Op.NOOP) {
value.set(value.get() * 10 + Integer.parseInt(s));
} else {
stackValue.set(value.get());
value.set(Integer.parseInt(s));
stackOp = curOp;
curOp = Op.NOOP;
}
}
});
}
private void makeClearButton(Button button) {
button.setStyle("-fx-base: ivory;");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
value.set(0);
}
});
}
private void makeBackspaceButton(Button button) {
button.setStyle("-fx-base: ivory;");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
value.set(value.get());
}
});
}
private void makeSquarerootButton(Button button) {
button.setStyle("-fx-base: ivory;");
}
private void makePercentageButton(Button button) {
button.setStyle("-fx-base: ivory;");
}
private void makePointButton(Button button) {
button.setStyle("-fx-base: ivory;");
}
private void makeEqualsButton(Button button) {
button.setStyle("-fx-base: ivory;");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
switch (stackOp) {
case ADD:
value.set(stackValue.get() + value.get());
break;
case SUBTRACT:
value.set(stackValue.get() - value.get());
break;
case MULTIPLY:
value.set(stackValue.get() * value.get());
break;
case DIVIDE:
value.set(stackValue.get() / value.get());
break;
}
}
});
}
if (OutputTextField.getText().length()>0){
StringBuffer sb = new StringBuffer(OutputTextField.getText());
sb = sb.deleteCharAt(OutputTextField.getText().length()-1);
OutputTextField.setText(sb.toString());
}
}
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.property.*;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.input.KeyEvent;
导入javafx.scene.layout.*;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
导入java.util.HashMap;
导入java.util.Map;
//一个简单的JavaFX计算器。
公共类Calcy扩展应用程序{
//实现模板,如果我希望按钮看起来像
私有静态最终字符串[][]模板={
{“c”、“%”、“√", "←"},
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"}
};
私有最终映射加速器=新HashMap();
私有DoubleProperty stackValue=新的SimpleDoubleProperty();
私有DoubleProperty值=新的SimpleDoubleProperty();
私有枚举操作{
NOOP,加,减,乘,除
}
私有Op curOp=Op.NOOP;
私有Op stackOp=Op.NOOP;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
最终文本字段屏幕=createScreen();
最终TilePane按钮=createButtons();
setTitle(“感谢上帝给了我一个计算器!”);
stage.initStyle(StageStyle.UTILITY);
阶段。可设置大小(假);
stage.setScene(新场景(创建布局(屏幕、按钮));
stage.show();
}
专用VBox createLayout(文本字段屏幕、TilePane按钮){
最终VBox布局=新的VBox(20);
布局。设置对齐(位置中心);
layout.setStyle(“-fx背景色:floralwhite;-fx填充:20;-fx字体大小:20;”);
layout.getChildren().setAll(屏幕、按钮);
手动加速器(布局);
screen.prefWidthProperty().bind(buttons.widthProperty());
返回布局;
}
专用空心手动加速器(VBox布局){
layout.addEventFilter(按下KeyEvent.KEY_,新建EventHandler()){
@凌驾
公共无效句柄(KeyEvent KeyEvent){
按钮激活=accelerators.get(keyEvent.getText());
如果(已激活!=null){
激活。火();
}
}
});
}
私有文本字段createScreen(){
最终文本字段屏幕=新文本字段();
屏幕设置样式(“-fx背景色:浅灰色;”);
屏幕设置对齐(位置居中\右侧);
screen.setEditable(假);
screen.textProperty().bind(Bindings.format(“%.2f”,value));
返回屏幕;
}
私有TilePane createButtons(){
TilePane按钮=新的TilePane();
按钮。设置间隙(7);
按钮。setHgap(7);
buttons.setPrefColumns(模板[0].length);
for(字符串[]r:模板){
for(字符串s:r){
buttons.getChildren().add(createButton);
}
}
返回按钮;
}
私有按钮createButton(最终字符串s){
按钮按钮=制作标准按钮;
如果(s.匹配(“[0-9]”){
makeNumericButton(s,button);
}否则{
final ObjectProperty triggerOp=确定词和;
if(triggerOp.get()!=Op.NOOP){
按钮(按钮、触发器);
}否则,如果(“c”等于(s)){
makeClearButton(按钮);
}如果(“=”。等于(s)){
MakequalButton(按钮);
}否则如果(”←“.equals(s)){
makeBackspaceButton(按钮);
}如果(“%”等于(s)){
makePercentageButton(按钮);
}否则如果(”√“.equals(s)){
makeSquarerootButton(按钮);
}如果(“..”等于(s)){
makePointButton(按钮);
}
}
返回按钮;
}
private ObjectProperty DeterminationPerand(字符串s){
final ObjectProperty triggerOp=新的SimpleObject属性(Op.NOOP);
开关{
格“+”:
triggerOp.set(Op.ADD);
打破
案例“-”:
触发器集合(运算减法);
打破
案例“*”:
triggerOp.set(Op.MULTIPLY);
打破
案例“/:
triggerOp.set(Op.DIVIDE);
打破
大小写“%”:
}
返回触发器;
}
私有void makeOperationButton(按钮按钮,最终ObjectProperty触发器){
button.setStyle(“-fx base:ivory;”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
curOp=triggerOp.get();
}
});
}
私有按钮makeStandardButton(字符串s){
按钮=新按钮;
button.setStyle(“-fx base:ivory;”);
加速器。放置(s,按钮);
按钮.setMaxSize(Double.MAX\u值,Double.MAX\u值);
返回按钮;
}
私有void makeNumericButton(最终字符串s,按钮){
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
如果(curOp==Op.NOOP){
value.set(value.get()*10+Integer.parseInt);
}否则{
stackValue.set(value.get());
value.set(Integer.parseInt(s));
stackOp=curOp;
curOp=Op.NOOP;
}
}
});
}
私有void makeClearButton(按钮){
按钮。设置样式(“-fx底座:象牙色
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
value.set((int)value.get()/10 +
/**
* To get the decimal part
*/
value.get() - (int)value.get());
}
});
if (OutputTextField.getText().length()>0){
StringBuffer sb = new StringBuffer(OutputTextField.getText());
sb = sb.deleteCharAt(OutputTextField.getText().length()-1);
OutputTextField.setText(sb.toString());
}
}