在java fxml中通过数字时钟的文本字段传递变量字符串
我正在制作一个带有数字和模拟时钟的秒表,但在打印数字时钟值时遇到了问题,主要是因为我不确定如何将字符串传递给FXML 为什么当我尝试在fxml中为textfield设置id=“digitalText”时,程序会失败,但如果我使用text=“00:00”编写它,它会正常工作(只使用带有00:00的文本,当然不会更新数字时钟)。我希望给它一个id可以在里面打印出值 控制器在java fxml中通过数字时钟的文本字段传递变量字符串,java,fxml,Java,Fxml,我正在制作一个带有数字和模拟时钟的秒表,但在打印数字时钟值时遇到了问题,主要是因为我不确定如何将字符串传递给FXML 为什么当我尝试在fxml中为textfield设置id=“digitalText”时,程序会失败,但如果我使用text=“00:00”编写它,它会正常工作(只使用带有00:00的文本,当然不会更新数字时钟)。我希望给它一个id可以在里面打印出值 控制器 package tds7xbstopwatchfxml; import java.awt.TextField; import
package tds7xbstopwatchfxml;
import java.awt.TextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
public class FXMLDocumentController implements Initializable {
@FXML
private ImageView clockFace;
@FXML
private ImageView hand;
@FXML
private TextField digitalText;
String digital;
Double rotation = 0.0;
Integer ms = 00;
Integer mm = 00;
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(1000), (ActionEvent actionEvent) -> {
updateTimer();
updateDigital();
})
);
@FXML
private void stopButton(ActionEvent event) {
timeline.stop();
}
@FXML
private void startButton(ActionEvent event) {
timeline.play();
}
@FXML
private void resetButton(ActionEvent event) {
rotation = 0.0;
ms = 0;
mm = 0;
hand.setRotate(0);
digital="00:00";
timeline.stop();
}
public void updateTimer() {
ms += 1000;
if (ms >= 60000) {
ms = 00;
mm++;
}
rotation = (ms / 60000.0) * 360.0;
hand.setRotate(rotation);
}
public void updateDigital(){
if(ms/1000<10 && mm<10){
digital=("0" + String.valueOf(mm) + ":" + "0" + String.valueOf(ms/1000));
}
if(ms/1000>=10 && mm <10){
digital=("0" + String.valueOf(mm) + ":" + "" + String.valueOf(ms/1000));
}
if(mm>=10 && ms/1000<10){
digital=("" + String.valueOf(mm) + ":" + "0" + String.valueOf(ms/1000));
}
if(mm>=10 && ms/1000>=10){
digital=("" + String.valueOf(mm) + ":" + "" + String.valueOf(ms/1000));
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
clockFace.setImage(new Image(this.getClass().getResourceAsStream("clockface.png")));
hand.setImage(new Image(this.getClass().getResourceAsStream("hand.png")));
timeline.setCycleCount(Animation.INDEFINITE);
digitalText.setText(digital);
}
}
包tds7xbstopwatchfxml;
导入java.awt.TextField;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.animation.animation;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.util.Duration;
公共类FXMLDocumentController实现可初始化{
@FXML
私有图像视图时钟面;
@FXML
私人影像查看手;
@FXML
私有文本字段数字文本;
字符串数字;
双旋转=0.0;
整数ms=00;
整数mm=00;
时间线=新时间线(
新关键帧(持续时间.millis(1000),(ActionEvent)->{
updateTimer();
updateDigital();
})
);
@FXML
私有void停止按钮(ActionEvent事件){
timeline.stop();
}
@FXML
私有无效开始按钮(ActionEvent事件){
timeline.play();
}
@FXML
私有无效重置按钮(ActionEvent事件){
旋转=0.0;
ms=0;
mm=0;
手动旋转(0);
数字=“00:00”;
timeline.stop();
}
公共void updateTimer(){
ms+=1000;
如果(毫秒>=60000){
ms=00;
mm++;
}
旋转=(ms/60000.0)*360.0;
手动设置旋转(旋转);
}
public void updateDigital(){
如果(ms/1000=10){
数字=(“+String.valueOf(mm)+”:“+”+String.valueOf(ms/1000));
}
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
setImage(新图像(this.getClass().getResourceAsStream(“clockFace.png”));
setImage(新图像(this.getClass().getResourceAsStream(“hand.png”));
timeline.setCycleCount(Animation.unfinite);
setText(数字);
}
}
我知道有更好的方法来实现数字时钟的代码,但我主要想知道如何传递要打印的更改字符串。数字文本.setText(数字);会导致它崩溃
以及fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tds7xbstopwatchfxml.FXMLDocumentController">
<stylesheets>
<URL value="@stopwatchui.css" />
</stylesheets>
<children>
<StackPane layoutY="94.0" prefHeight="150.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="175.0">
<children>
<ImageView fx:id="clockFace" fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="hand" fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
</children>
</StackPane>
<TextField fx:id="digitalText" alignment="CENTER" editable="false" layoutX="204.0" layoutY="123.0" prefHeight="43.0" prefWidth="194.0">
<font>
<Font name="Arial" size="23.0" />
</font>
</TextField>
<Button fx:id="stopbutton" layoutX="280.0" layoutY="75.0" onAction="#stopButton" text="Stop" />
<Button fx:id="startbutton" layoutX="204.0" layoutY="75.0" onAction="#startButton" text="Start" />
<Button fx:id="resetbutton" layoutX="353.0" layoutY="75.0" onAction="#resetButton" text="Reset" />
</children>
</AnchorPane>
您导入了Swing文本字段,而不是JavaFX文本字段 改变
import java.awt.TextField
到
您导入了Swing文本字段,而不是JavaFX文本字段 改变
import java.awt.TextField
到