如何创建一个需要一两个数的javafx程序,并从另一个类中调用该信息的方法?

时间:2016-02-18 21:12:46

标签: java javafx javafx-8

def warning
  term_type == TERM_MONTH ? MONTHLY_WARNING_1 : ANNUAL_WARNING_1
end

warning.from_now # first set warning or whatever it does
send_notice      # now send the notice

这是一种更简单的方法。我想创建一个控制器类,让用户输入第一个数字,按添加按钮,输入第二个数字,按回车键,然后将返回值的字符串表示打印到标签。我不确定如何使用控制器调用正确的方法。我可能也许我应该有一个方法来检查特定文本(例如"添加")从按下的按钮并执行正确的方法,但我觉得这不是正确的方法。 我基本上希望输入的第一个数字被视为"这个"每个方法的指针,以及任何后续数字作为参数。

2 个答案:

答案 0 :(得分:0)

您必须在按钮上设置动作侦听器。使用JavaFX 8的示例:

Button button = new Button();
button.setText("Button Text");
button.setOnAction((ActionEvent event) -> {
    System.out.println("Button Clicked!");
});

对于ENTER输入,您需要向节点添加事件处理程序。 E.g:

Scene scene = new Scene(root);
scene.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent key) -> {
    if(key.getCode().equals(KeyCode.ENTER)) {
        System.out.println("ENTER pressed");
    }
}

答案 1 :(得分:0)

你可以做这样的事情(这显然是不完整的)希望能指出你的方向:

long runningTotal = 0;
Button plusButton = new Button("+");
Button enterButton = new Button("Enter");
TextField tf = new TextField();
Label displayLabel = new Label();

//Setup you UI here

plusButton.setOnAction(event -> {
    String stringValue = tx.getText();
    long value = Long.parseLong(stringValue);
    runningTotal = runningTotal + value;
});

enterButton.setOnAction(event -> {
    //if you need to remember your last pressed button, you could have done that also, then perform that action here
    displayLabel.setText("" + runningTotal);
});
相关问题