【问题标题】:Update FXML application along with console [duplicate]与控制台一起更新 FXML 应用程序 [重复]
【发布时间】:2018-01-18 22:34:16
【问题描述】:

我对 Java 编程很陌生,但我想制作一个简单的向上或向下计数器(基于输入值)。我为我的代码创建了一个 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>

【问题讨论】:

  • MainJavaFX 格式似乎不正确,或者这可能是您在尝试制作JavaFx 应用程序之前使用的代码?
  • 不要用 Thread.sleep() 阻塞 FX 应用程序线程(你会阻止 UI 被渲染)。例如,请参阅stackoverflow.com/questions/9966136/…
  • 如果你在你的JavaFX项目中使用UpOrDownCounter.java,它不应该有main方法。
  • 你应该澄清你的问题!您没有按照需要启动的方式启动 JavaFX 应用程序,或者您没有提供所有代码。所以,我正在降级你的问题。如果您确实需要有关 JavaFX 应用程序线程的帮助,您首先需要启动它。好吧,要启动 JavaFX 应用程序,请看这里:docs.oracle.com/javase/8/javafx/get-started-tutorial/…,然后,如果您编辑您的问题,我可以拒绝降级投票。顺便说一句,您不需要如此复杂的逻辑来构建 Up 或 Down 计数器。

标签: java javafx


【解决方案1】:

您将不得不稍微使用一下您的代码,但这里是您解决第一个问题的方法。使用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();
    }
}

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2021-10-03
    • 2014-11-21
    相关资源
    最近更新 更多