我正在尝试创建一个非常基本的JavaFX计算器,但我不确定如何实现退格。
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());
}
});
}
我不能为我的生活想到一种方法来删除输入的最后一个值。因此,如果输入值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;
}
}
});
}
}
答案 0 :(得分:1)
您可以在退格按钮的操作方法中添加以下行。这只是我做的正常操作。
获取号码,但输入的最后一个号码可以通过
访问(int)value.get()/10
稍后,可以通过
访问小数部分value.get() - (int)value.get())
所以这就是
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());
}
});
小数部分仍然无法处理您的计算器,也可以修复它。完成后,您必须更改方法以考虑小数点前后的值!
答案 1 :(得分:0)
private void BackButtonActionPerformed(java.awt.event.ActionEvent evt){
if (OutputTextField.getText().length()>0){
StringBuffer sb = new StringBuffer(OutputTextField.getText());
sb = sb.deleteCharAt(OutputTextField.getText().length()-1);
OutputTextField.setText(sb.toString());
}
}