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

时间:2016-03-09 01:40:58

标签: java javafx stack

我正在使用JAVA FX和堆栈制作基本计算器。当我点击任何按钮时,我得到屏幕上显示的那个数字,但我希望在点击各种按钮时显示一个连续的表达,如果我点击'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();
  }

}

![Exmaple Image] http://imgur.com/DUOU4vH

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:-3)

有一个简单的解决方案。 而不是使用
    calculator_screen.setText(key_values [A] [B]);

使用,     calculator_screen.appendText(key_values [A] [B]);

希望这会对你有所帮助。

相关问题