我正在制作一个带有数字和模拟时钟的秒表,但是打印数字时钟值时遇到了麻烦,主要是因为我不确定如何将字符串传递给FXML。
为什么当我尝试使用id =“digitalText”作为fxml中的文本字段时,程序会失败,但如果我使用text =“00:00”来编写它,它将正常工作(只需要一个文本00:00虽然没有更新数字时钟当然)。我希望给它id会打印出里面的值。
控制器
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);
}
}
我知道有更好的方法来实现数字时钟的代码,但主要是我想了解传递要更改的字符串以进行打印。 digitalText.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>
答案 0 :(得分:0)
您导入了Swing TextField,而不是JavaFX文本字段
更改
import java.awt.TextField
到
import java.scene.control.TextField;