如何在javafx中不断更新标签?
我试图在JavaFX中每秒更新标签的文本。有没有像Swing中那样的repaint()方法可以让我在JavaFX中实现这一点。我正在尝试让我的应用程序显示时间,并用新的当前时间每秒更新标签 谢谢大家!!我的代码如下如何在javafx中不断更新标签?,java,user-interface,javafx,Java,User Interface,Javafx,我试图在JavaFX中每秒更新标签的文本。有没有像Swing中那样的repaint()方法可以让我在JavaFX中实现这一点。我正在尝试让我的应用程序显示时间,并用新的当前时间每秒更新标签 谢谢大家!!我的代码如下 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalTime; import java.util.Calendar; import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import javax.management.timer.Timer;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TimeAppGraphics extends Application
{
private static final String Indefinite = null;
public static void main(String[] args)
{
// start app by launching software
launch(args);
}
@Override // overides the parent class
public void start(Stage myStage)
{
Button exitButton = new Button("Exit!"); // creates an exit button for the app.
myStage.setTitle("Bell Application"); //sets the title of the application to Bell app
exitButton.setFont(Font.font("Impact" ,FontPosture.ITALIC, 24)); //sets the font for the button
exitButton.setLayoutX(215);
exitButton.setLayoutY(370);
//This method create the event for the button, to close the application.
exitButton.setOnAction(ae -> {
// TODO Auto-generated method stub
try
{
Platform.exit(); //exits app.
}
catch(Exception e)
{
System.out.println(e.getMessage()); //prints error if there is one.
}
});
//Now to add an Image to the stage
myStage.setHeight(500); // sets the panel to be 500 by 500 pixels.
myStage.setWidth(500);
LocalTime time = LocalTime.now();
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
FlowPane fp = new FlowPane();
DateFormat timeFormat = new SimpleDateFormat( "HH:mm" );
Label timeLabel = new Label();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
final int millis = 50;
Timer tmr = new Timer();
tmr.start();
boolean bool = true;
timeLabel.setTextFill(Color.LAWNGREEN);
timeLabel.setFont(new Font("Impact", 24));
timeLabel.setLayoutX(0);
timeLabel.setLayoutY(100);
//Period Label
Calendar calender = Calendar.getInstance();
Label periodLabel = new Label();
int weekNum = calender.get(Calendar.WEEK_OF_YEAR);
if(weekNum % 2 != 0)
{
int dayNum = calender.get(Calendar.DAY_OF_WEEK);
switch(dayNum)
{
case 2:
String dayType = "A Day";
if(hour >= 7 && minute >= 35 && hour <= 9 && minute <= 11)
{
String period = "A Period";
periodLabel.setText(period);
}
}
}
try
{
//For the image
//_______________________________________________________________________________________________
//this try catch catches errors for the file.
Image bell = new Image("https://media.istockphoto.com/vectors/school-bell-vector-id526811999?k=6&m=526811999&s=612x612&w=0&h=7XrnFiPTYW5aBjHQwPL5eGpK01ZFKs61q6N2yks60Wg=");
ImageView bellDisplay = new ImageView(bell); // sets a new imageview, which paints the image to the screen.
bellDisplay.setX(175); //positions the x axis of the image
bellDisplay.setY(175); // positions the image on the y axis
//set fit
bellDisplay.setFitHeight(150);
bellDisplay.setFitWidth(150);
//set preseve ratio
bellDisplay.setPreserveRatio(true);
//______________________________________________________________________________________________
Date date = new Date();
time = LocalTime.now();
hour = time.getHour();
minute = time.getMinute();
second = time.getSecond();
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(1),
actionEvent -> timeLabel.setText("Time: " + simpleDateFormat.format(date))
));
timeline.getKeyFrames();
//fp.getChildren().add(timeLabel);
timeline.setCycleCount(Animation.INDEFINITE);//Repeat this 100 times
timeline.play();
//fp.getChildren().add(exitButton);
//fp.getChildren().add(bellDisplay);
Group objects = new Group(exitButton, bellDisplay, timeLabel); //groups the objects together to be shown.
Scene scene = new Scene(objects, 1000,1000 , Color.ROYALBLUE); // creates a scene to be displayed.
myStage.setScene(scene); //creates the program
}
catch(Exception e)
{
System.out.println(e.getMessage()); // prints error
}
myStage.show();
}
}
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.time.LocalTime;
导入java.util.Calendar;
导入java.util.Date;
导入javax.management.timer.timer;
导入javafx.animation.animation;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.FlowPane;
导入javafx.scene.paint.Color;
导入javafx.scene.text.Font;
导入javafx.scene.text.fontpostation;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类TimeAppGraphics扩展了应用程序
{
私有静态最终字符串不定=null;
公共静态void main(字符串[]args)
{
//通过启动软件启动应用程序
发射(args);
}
@重写//覆盖父类
公共无效开始(舞台myStage)
{
Button exitButton=新建按钮(“退出!”;//为应用程序创建退出按钮。
setTitle(“Bell应用程序”);//将应用程序的标题设置为Bell应用程序
exitButton.setFont(Font.Font(“Impact”,fontpostation.ITALIC,24));//设置按钮的字体
exitButton.setLayoutX(215);
exitButton.setLayoutY(370);
//此方法为按钮创建事件,以关闭应用程序。
exitButton.setOnAction(ae->{
//TODO自动生成的方法存根
尝试
{
Platform.exit();//退出应用程序。
}
捕获(例外e)
{
System.out.println(e.getMessage());//如果存在错误,则打印错误。
}
});
//现在将图像添加到舞台
setHeight(500);//将面板设置为500×500像素。
myStage.setWidth(500);
LocalTime=LocalTime.now();
int hour=time.getHour();
int minute=time.getMinute();
int second=time.getSecond();
FlowPane fp=新的FlowPane();
DateFormat timeFormat=新的SimpleDateFormat(“HH:mm”);
标签timeLabel=新标签();
SimpleDataFormat SimpleDataFormat=新SimpleDataFormat(“HH:mm:ss”);
最终整数毫秒=50;
定时器tmr=新定时器();
tmr.start();
布尔布尔布尔=真;
timeLabel.setTextFill(Color.LAWNGREEN);
timeLabel.setFont(新字体(“Impact”,24));
timeLabel.setLayoutX(0);
时间标签设置布局(100);
//周期标签
日历日历=Calendar.getInstance();
标签周期标签=新标签();
int weekNum=Calendar.get(Calendar.WEEK/u/u年);
如果(周数%2!=0)
{
int dayNum=Calendar.get(Calendar.DAY/u周);
开关(dayNum)
{
案例2:
String dayType=“一天”;
如果(小时>=7&&minute>=35&&hour您的代码工作正常,则问题在于您的日期对象,在将新值设置为timeLabel
后,您还需要获取新的DateAndTime
:
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
Date date = new Date();
timeLabel.setText("Time: " + simpleDateFormat.format(date));
}
}));
Timeline Timeline=new Timeline(新的关键帧)(持续时间.秒(1),新的EventHandler您的代码工作正常问题在于日期对象,在将新值设置为timeLabel
后,您还需要获取新的DateAndTime
:
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
Date date = new Date();
timeLabel.setText("Time: " + simpleDateFormat.format(date));
}
}));
Timeline Timeline=新的时间线(新的关键帧)(持续时间。秒(1),new EventHandler@kleopatra我刚刚添加了他在代码中写的有用的东西!谢谢!@LiamMcIntyre如果有用,请投票并接受它为正确的answer@kleopatra我还使用Java8添加了日期和时间Api@kleopatra我刚刚在他的代码中添加了一些有用的东西!谢谢!@LiamMcIntyre如果有用,请投赞成票并认为它是正确的answer@kleopatra我还添加了使用java 8日期和时间API不要使用旧的日期相关类。你能清理一下这个例子吗?有很多未使用的导入、声明没有使用的字段、计时器似乎根本没有使用,等等。请发布演示问题的代码lem可以短得多。看到了吗?这回答了你的问题吗?不要使用旧的日期相关类。你能清理一下这个例子吗?有很多未使用的导入,声明没有使用的字段,计时器似乎根本没有使用,等等。请发布。演示问题的代码可以短得多。参见D这能回答你的问题吗?