使用SceneBuilder在JavaFX项目的开始处调用函数

使用SceneBuilder在JavaFX项目的开始处调用函数

本文介绍了使用SceneBuilder在JavaFX项目的开始处调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SceneBuilder为我的项目创建了一个GUI,并设置了所有按钮ID,并为FXML文件创建了一个控制器.我想在启动时和整个程序中的文本区域中运行一个实时时钟.这是我第一次使用FXML在JavaFX中创建项目,因此我对应该在何处放置此代码感到困惑.通常,该代码可以在没有FXML的简单程序中工作,并且是以下代码:

I created a GUI for my project using SceneBuilder and I set up all the button ID's and created a controller for the FXML file. I want to have a live clock running in the text area on launch and throughout the program. This is my first time using FXML to create a project in JavaFX so I'm confused as to where I should place this code. Normally the code works in a simple program without FXML and it is this code:

package com.example;
import example;

public class Layout extends Application {

         TextArea clock;

public void start(Stage stage) throws FileNotFoundException {

         clock = new TextArea();
         clock.setEditable(false);
          BorderPane bp = new BorderPane();
          bp.setTop(clock);

          refreshClock();

         Scene scene = new Scene(bp);

        stage.setScene(scene);
        stage.show();
   }
}

  private void refreshClock()
    {
        Thread refreshClock = new Thread()
           {
              public void run()
              {
                while (true)
                {
                    Date dte = new Date();

                    String topMenuStr = "       " + dte.toString();
                    clock.setText(topMenuStr);

                    try
                    {
                       sleep(3000L);
                    }
                    catch (InterruptedException e)
                    {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }

                }  // end while ( true )

            } // end run thread
         };

         refreshClock.start();
    }

当我尝试执行当前在我的Ui控制器类中的进度时,尽管我执行了什么代码,但是在Text Area中没有弹出任何内容,而且我不确定下一步该怎么做.此代码应该在我的主要.java文件中吗?这是我尝试过的:

When I attempt to do it my current progress in my Ui controller class, nothing pops up in the Text Area despite what code I do and I'm not sure what to do next. Should this code be in my main .java file? Here is what I tried:


package application;

import java.util.Date;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;

public class UiController {

    @FXML
    private TextArea clockTextArea;


private void refreshClock()
    {
        Thread refreshClock = new Thread()
           {
              public void run()
              {
                while (true)
                {
                    Date dte = new Date();

                    String topMenuStr = "       " + dte.toString();
                    clockTextArea.setText(topMenuStr);

                    try
                    {
                       sleep(3000L);
                    }
                    catch (InterruptedException e)
                    {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }

                }  // end while ( true )

            } // end run thread
         };

         refreshClock.start();
    }




    public void initialize() {

        refreshClock();

    }


推荐答案

在javafx中,您可以在initialize()方法中处理事件.

in javafx you work with events in the initialize() method.

例如button.setOnAction(e-> System.Exit(0));或者clock.onMouseClicked(e-> System.out.println("Test")));

E.g. button.setOnAction(e -> System.Exit(0)); orclock.onMouseClicked(e -> System.out.println("Test"));

这篇关于使用SceneBuilder在JavaFX项目的开始处调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:38