如何将其他类添加到当前阶段-JavaFX
我有一个javafx应用程序,可以显示一些信息 我是javafx新手,在尝试一些测试的同时试图理解一些东西 我想在应用程序顶部添加时钟,我发现 如何将此时钟(从源)添加到同一屏幕(屏幕顶部)上的当前应用程序?我的应用程序在如何将其他类添加到当前阶段-JavaFX,java,javafx,javafx-8,Java,Javafx,Javafx 8,我有一个javafx应用程序,可以显示一些信息 我是javafx新手,在尝试一些测试的同时试图理解一些东西 我想在应用程序顶部添加时钟,我发现 如何将此时钟(从源)添加到同一屏幕(屏幕顶部)上的当前应用程序?我的应用程序在main.java上使用下一个代码(我也在使用FXML文件,并通过场景生成器进行编辑): 包装样品; 导入javafx.application.application; 导入javafx.fxml.fxmloader; 导入javafx.scene.Parent; 导入java
main.java
上使用下一个代码(我也在使用FXML文件,并通过场景生成器进行编辑):
包装样品;
导入javafx.application.application;
导入javafx.fxml.fxmloader;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.stage.stage;
导入net.sourceforge.zmanim.hebrewcalendar.JewishCalendar;
导入net.sourceforge.zmanim.hebrewcalendar.HebrewDateFormatter;
导入net.sourceforge.zmanim.hebrewcalendar.JewishCalendar;
导入net.sourceforge.zmanim.util.GeoLocation;
公共类主扩展应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
父根=FXMLLoader.load(getClass().getResource(“sample.fxml”);
Label lblShabat=(Label)root.lookup(“#shabat”);
Label lbldateHeb=(Label)root.lookup(“#dateHeb”);
JewishCalendar=新JewishCalendar();
israelCalendar.setInIsrael(true);//将日历设置为Israel
JewishCalendar chutsLaaretzCalendar=新JewishCalendar();
chutsLaaretzCalendar.setInIsrael(false);//实际上不需要,因为API默认为false
JewishCalendar jd=新的JewishCalendar();
HebrewDateFormatter hdf=新的HebrewDateFormatter();
hdf.setHebrewFormat(真);
对于(int i=0;i<14;i++){
israelCalendar.forward();//将日期向前滚动一天
//chutsLaaretzCalendar.forward();//将日期向前滚动一天
如果(Calendar.getDayOfWeek()==7){//忽略工作日
if(lblShabat!=null)lblShabat.setText(hdf.formatYomTov(jd));//hdf.formatParsha(日历)
//hdf.formatYomTov(jd)
}
}
字符串cholHamoedSuccos=“להעדסוות”;
if(hdf.formatYomTov(jd)=cholHamoedSuccos){
字符串image=Main.class.getResource(“Dollarphotoclub_91486993.jpg”).toExternalForm();
setStyle(“-fx背景图像:url(“+image+”);”+
“-fx背景位置:居中;”+
“-fx背景重复:拉伸;”;
}
如果(lbldateHeb!=null)lbldateHeb.setText(hdf.format(jd));
setTitle(“你好世界”);
初级阶段。设置场景(新场景(根,300275));
primaryStage.setFullScreen(真);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
这就是你想要的吗?然后将其添加到
窗格中
也许有一种更好的方法不需要太多重构,但我重构了时钟,以便将函数createLayout()与时钟的start()函数分离。在SceneBuilder中,添加一个子场景,在其中放置时钟并为其设置id,我使用了clocksubscene
。在controller initialize()函数中,创建布局并将clocksubscene的根设置为layout。还将clock.css添加到主场景的样式中
重构的Clock.java(未修改的部分):
在主启动程序中:
Scene scene = new Scene(root, 300, 275);
scene.getStylesheets().add(Clock.getResource("clock.css"));
primaryStage.setScene(scene);
我将进一步重构它,使
createLayout()
方法位于单独的类中,而不是应用程序
子类。(我可能会调用该类Clock
和原始应用程序子类ClockApp
),这样您就不需要应用程序和启动(…)
方法等的多余API,如果你不使用它。我想如果你真的尝试它,你可能会发现它不像你想象的那么容易工作。。。首先,你不能在两个不同的场景中有一个节点(我想你会得到一个例外)。第二,首先,你如何进入舞台
TheStageOfthesource.getScene().getRoot();//will give you the root pane
public void start(final Stage stage) throws Exception {
Parent layout = createLayout();
final Scene scene = new Scene(layout, Color.TRANSPARENT);
scene.getStylesheets().add(getResource("clock.css"));
// show the scene.
stage.initStyle(StageStyle.TRANSPARENT);
stage.setScene(scene);
stage.show();
}
public Parent createLayout() {
// construct the analogueClock pieces.
final Circle face = new Circle(100, 100, 100);
face.setId("face");
final Label brand = new Label("Splotch");
brand.setId("brand");
brand.layoutXProperty().bind(face.centerXProperty().subtract(brand.widthProperty().divide(2)));
brand.layoutYProperty().bind(face.centerYProperty().add(face.radiusProperty().divide(2)));
final Line hourHand = new Line(0, 0, 0, -50);
hourHand.setTranslateX(100);
hourHand.setTranslateY(100);
hourHand.setId("hourHand");
final Line minuteHand = new Line(0, 0, 0, -75);
minuteHand.setTranslateX(100);
minuteHand.setTranslateY(100);
minuteHand.setId("minuteHand");
final Line secondHand = new Line(0, 15, 0, -88);
secondHand.setTranslateX(100);
secondHand.setTranslateY(100);
secondHand.setId("secondHand");
final Circle spindle = new Circle(100, 100, 5);
spindle.setId("spindle");
Group ticks = new Group();
for (int i = 0; i < 12; i++) {
Line tick = new Line(0, -83, 0, -93);
tick.setTranslateX(100);
tick.setTranslateY(100);
tick.getStyleClass().add("tick");
tick.getTransforms().add(new Rotate(i * (360 / 12)));
ticks.getChildren().add(tick);
}
final Group analogueClock = new Group(face, brand, ticks, spindle, hourHand, minuteHand, secondHand);
// construct the digitalClock pieces.
final Label digitalClock = new Label();
digitalClock.setId("digitalClock");
// determine the starting time.
Calendar calendar = GregorianCalendar.getInstance();
final double seedSecondDegrees = calendar.get(Calendar.SECOND) * (360 / 60);
final double seedMinuteDegrees = (calendar.get(Calendar.MINUTE) + seedSecondDegrees / 360.0) * (360 / 60);
final double seedHourDegrees = (calendar.get(Calendar.HOUR) + seedMinuteDegrees / 360.0) * (360 / 12);
// define rotations to map the analogueClock to the current time.
final Rotate hourRotate = new Rotate(seedHourDegrees);
final Rotate minuteRotate = new Rotate(seedMinuteDegrees);
final Rotate secondRotate = new Rotate(seedSecondDegrees);
hourHand.getTransforms().add(hourRotate);
minuteHand.getTransforms().add(minuteRotate);
secondHand.getTransforms().add(secondRotate);
// the hour hand rotates twice a day.
final Timeline hourTime = new Timeline(
new KeyFrame(
Duration.hours(12),
new KeyValue(
hourRotate.angleProperty(),
360 + seedHourDegrees,
Interpolator.LINEAR
)
)
);
// the minute hand rotates once an hour.
final Timeline minuteTime = new Timeline(
new KeyFrame(
Duration.minutes(60),
new KeyValue(
minuteRotate.angleProperty(),
360 + seedMinuteDegrees,
Interpolator.LINEAR
)
)
);
// move second hand rotates once a minute.
final Timeline secondTime = new Timeline(
new KeyFrame(
Duration.seconds(60),
new KeyValue(
secondRotate.angleProperty(),
360 + seedSecondDegrees,
Interpolator.LINEAR
)
)
);
// the digital clock updates once a second.
final Timeline digitalTime = new Timeline(
new KeyFrame(Duration.seconds(0),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
Calendar calendar = GregorianCalendar.getInstance();
String hourString = pad(2, '0', calendar.get(Calendar.HOUR) == 0 ? "12" : calendar.get(Calendar.HOUR) + "");
String minuteString = pad(2, '0', calendar.get(Calendar.MINUTE) + "");
String secondString = pad(2, '0', calendar.get(Calendar.SECOND) + "");
String ampmString = calendar.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
digitalClock.setText(hourString + ":" + minuteString + ":" + secondString + " " + ampmString);
}
}
),
new KeyFrame(Duration.seconds(1))
);
// time never ends.
hourTime.setCycleCount(Animation.INDEFINITE);
minuteTime.setCycleCount(Animation.INDEFINITE);
secondTime.setCycleCount(Animation.INDEFINITE);
digitalTime.setCycleCount(Animation.INDEFINITE);
// start the analogueClock.
digitalTime.play();
secondTime.play();
minuteTime.play();
hourTime.play();
// add a glow effect whenever the mouse is positioned over the clock.
final Glow glow = new Glow();
analogueClock.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
analogueClock.setEffect(glow);
}
});
analogueClock.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
analogueClock.setEffect(null);
}
});
// layout the scene.
final VBox layout = new VBox();
layout.getChildren().addAll(analogueClock, digitalClock);
layout.setAlignment(Pos.CENTER);
return layout;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
Clock clock = new Clock();
clocksubscene.setRoot(clock.createLayout());
}
Scene scene = new Scene(root, 300, 275);
scene.getStylesheets().add(Clock.getResource("clock.css"));
primaryStage.setScene(scene);