java fx:单击按钮时显示文本

java fx:单击按钮时显示文本,java,javafx,stack,Java,Javafx,Stack,我正在使用JavaFX和堆栈制作一个基本的计算器。当我点击任何按钮时,屏幕上会显示该数字,但我希望在点击各个按钮时会显示一个连续的表达式,就像我点击“5”,然后点击“+”然后点击“3”,然后点击“-”然后点击1一样,所以当我点击各个按钮时,屏幕上应该显示“5+3-1”,而不是显示每个数字 public class Main extends Application { /*The keyboard key values*/ private static final String[][] key_

我正在使用JavaFX和堆栈制作一个基本的计算器。当我点击任何按钮时,屏幕上会显示该数字,但我希望在点击各个按钮时会显示一个连续的表达式,就像我点击“5”,然后点击“+”然后点击“3”,然后点击“-”然后点击1一样,所以当我点击各个按钮时,屏幕上应该显示“5+3-1”,而不是显示每个数字

public class Main extends Application {

/*The keyboard key values*/
private static final String[][] key_values = {
          { "7", "8", "9", "/" },
          { "4", "5", "6", "*" },
          { "1", "2", "3", "-" },
          { "0", "c", "=", "+" }
      };
private Button btn[][] = new Button[4][4]; //all the keys
TextField calculator_screen;  //the calculator screen


String num;
Stack <String>stack = new Stack<>();
MyStack s = new MyStack();
public static void main(String[] args) 
{ 
    launch(args);
    System.out.print("123456789");

}

  @Override public void start(Stage stage) {

     /*The outside layout*/
     final VBox layout = new VBox(30); //the size vertically

     /*The inside layout for keys or buttons*/
     TilePane keypad = new TilePane(); //even it is called keypad, it is a layout
     keypad.setVgap(7);
     keypad.setHgap(7); //set the gap between keys


    /*Create Calculator Screen */
    calculator_screen =  new TextField();
    calculator_screen.setStyle("-fx-background-color: #FFFFFF;"); //set the style of the screen
    calculator_screen.setAlignment(Pos.CENTER_RIGHT); //make the screen in the center of the calculator
    calculator_screen.setEditable(true); //make sure the screen cannot be typed in manually
    calculator_screen.setPrefWidth(500); //set the windth of the screen

    /*Create Calculator keyboard*/
    keypad.setPrefColumns(key_values[0].length); //set the preferred number of columns

    for (int i = 0; i < 4; i++) 
    {
      for (int j = 0; j < 4; j++) 
      {
        btn[i][j] = new Button(key_values[i][j]);
        final int a = i;
        final int b = j;

        /*Add button event*/
        btn[i][j].setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent event) {

                calculator_screen.setText(key_values[a][b]);

            }
        }
        );
        keypad.getChildren().add(btn[i][j]);
      }
    }



    /*Put the calculator screen and keypad into a VBox layout*/
    layout.setAlignment(Pos.CENTER);
    //layout.setStyle("-fx-background-color: #797983; -fx-padding: 20; -fx-font-size: 20;");
    layout.getChildren().addAll(calculator_screen, keypad);
    calculator_screen.prefWidthProperty().bind(keypad.widthProperty());


    /*Show the window*/
    stage.setTitle("Calculator");
    stage.initStyle(StageStyle.UTILITY);
    stage.setResizable(false);
    Scene scene = new Scene(layout);
    scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
    stage.setScene(scene);
    stage.show();
  }

}
public类主扩展应用程序{
/*键盘上的键值*/
私有静态最终字符串[][]密钥值={
{ "7", "8", "9", "/" },
{ "4", "5", "6", "*" },
{ "1", "2", "3", "-" },
{“0”,“c”,“=”,“+”}
};
私有按钮btn[][]=新按钮[4][4];//所有键
TextField calculator_screen;//计算器屏幕
字符串数;
堆栈=新堆栈();
MyStack s=新MyStack();
公共静态void main(字符串[]args)
{ 
发射(args);
系统输出打印(“123456789”);
}
@覆盖公共无效开始(阶段){
/*外部布局*/
最终VBox布局=新的VBox(30);//垂直大小
/*按键或按钮的内部布局*/
TilePane keypad=新的TilePane();//即使它被称为小键盘,它也是一种布局
键盘。设置间隙(7);
键盘。setHgap(7);//设置按键之间的间距
/*创建计算器屏幕*/
计算器屏幕=新文本字段();
calculator_screen.setStyle(“-fx背景色:35; FFFFFF;”)//设置屏幕的样式
calculator_screen.setAlignment(Pos.CENTER_RIGHT);//使屏幕位于计算器的中心
calculator_screen.setEditable(true);//确保不能手动键入屏幕
calculator_screen.setPrefWidth(500);//设置屏幕的宽度
/*创建计算器键盘*/
keypad.setPrefColumns(键值[0].length);//设置首选列数
对于(int i=0;i<4;i++)
{
对于(int j=0;j<4;j++)
{
btn[i][j]=新按钮(键值[i][j]);
最终int a=i;
最终int b=j;
/*添加按钮事件*/

btn[i][j].setOnAction(新事件处理程序)使用有一个简单的解决方案。 而不是使用
计算器屏幕.setText(键值[a][b])

使用, 计算器屏幕。追加文本(键值[a][b])

希望这对你有帮助