更新FXML应用程序以及控制台

时间:2018-01-16 14:56:57

标签: java javafx

我是Java编程的新手,但我想创建一个简单的Up或Down计数器(基于输入值)。我为我的代码创建了一个FXML应用程序,我希望计数器能够更新。假设我想要从1到6计数,我希望应用程序显示1,2,3,4,5,6。我已经设法在控制台中执行它,但FXML应用程序似乎暂停,而它是数数。

有人可以帮我吗?以下是我的所有代码:

UpOrDownCounter.java

package upOrDownCounter;

public class UpOrDownCounter {

private int stop;
private int start;
private int counter = 0;

public UpOrDownCounter(int start, int stop){
    this.stop = stop;
    this.start = start;

    if(start > stop) {
        counter = start + 1;
    }else {
        counter = start - 1;
    }

    if(start == stop) {
        throw new IllegalArgumentException("Start and stop cannot be the same value");
    }
}

int getCounter() {
    return counter;
}

boolean count() {
    if(start > stop) {
        if(getCounter() != stop) {
            counter = counter - 1;
            return true;
        }
    }else {
        if(getCounter() != stop) {
            counter = counter + 1;
            return true;
        }
    }
    return false;
}

public static void main(String[] args) {
    UpOrDownCounter cd = new UpOrDownCounter(6, 6);
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
    System.out.println(cd.getCounter());
    System.out.println(cd.count());
  }
}

UpOrDownCounterController.java

package upOrDownCounter;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class UpOrDownCounterController {
UpOrDownCounter cd;

@FXML TextField startValue;
@FXML TextField stopValue;
@FXML TextField counterOutput;

@FXML
void handleClick() {
    String startString = startValue.getText();
    int start = Integer.valueOf(startString);
    String stopString = stopValue.getText();
    int stop = Integer.valueOf(stopString);

    cd = new UpOrDownCounter(start, stop);

    while(cd.count()) {
        try {
            System.out.println("Counter: " + String.valueOf(cd.getCounter()));
            counterOutput.setText(String.valueOf(cd.getCounter()));
            System.out.println(String.valueOf(cd.getCounter()));
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
      }
   }
}

UpOrDownCounter.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>


<VBox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9" fx:controller="upOrDownCounter.UpOrDownCounterController">
 <children>
  <TextField fx:id="startValue" promptText="Start" />
  <TextField fx:id="stopValue" promptText="Stop" />
  <Button onAction="#handleClick" mnemonicParsing="false" prefHeight="37.0" prefWidth="196.0" text="Start countdown" />
  <TextField fx:id="counterOutput" prefHeight="32.0" prefWidth="196.0" />
 </children>
</VBox>

1 个答案:

答案 0 :(得分:0)

您将不得不稍微使用您的代码,但这是您解决第一个问题的方法。使用Timeline。使用this代码更改了答案。

    //Duration.seconds(1) tells this Timeline to run every second
    Timeline oneSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>()
    {

        @Override
        public void handle(ActionEvent event)
        {
            System.out.println("Counter: " + String.valueOf(cd.getCounter()));
            counterOutput.setText(String.valueOf(cd.getCounter()));
            System.out.println(String.valueOf(cd.getCounter()));
            cd.count();
        }
    }));

    oneSecondsWonder.setCycleCount(start - stop);//This tells the TimeLine to repeat itself a certain number of times.
    oneSecondsWonder.play();//This starts the Timeline.
  

完整代码

package upOrDownCounter;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class UpOrDownCounterController {
    UpOrDownCounter cd;

    @FXML
    TextField startValue;
    @FXML
    TextField stopValue;
    @FXML
    TextField counterOutput;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

    }

    @FXML
    void handleClick()
    {
        String startString = startValue.getText();
        int start = Integer.valueOf(startString);
        String stopString = stopValue.getText();
        int stop = Integer.valueOf(stopString);

        cd = new UpOrDownCounter(start, stop);

        Timeline oneSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>()
        {

            @Override
            public void handle(ActionEvent event)
            {
                System.out.println("Counter: " + String.valueOf(cd.getCounter()));
                counterOutput.setText(String.valueOf(cd.getCounter()));
                System.out.println(String.valueOf(cd.getCounter()));
                cd.count();
                if (cd.getCounter() == stop) {

                }
            }
        }));

        oneSecondsWonder.setCycleCount(start - stop);
        oneSecondsWonder.play();
    }
}