FireworkRocket 2023-12-23 16:41 采纳率: 100%
浏览 7
已结题

JavaFX启动时更新Label内容

我想在启动时就设置更新Label内容,该怎么写?
Main类


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
        Parent root = loader.load();
        Controller controller = loader.getController(); // 获取控制器实例
        // 显示场景和舞台
        Scene scene = new Scene(root, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

控制器


import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class Controller {
    @FXML
    public Label globalLabel; // 全局的JavaFXLabel变量

    // 其他方法...
}

FXML


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.FireworkRocket.Controller">
   <children>
      <Label fx:id="globalLabel" text="初始内容" />
   </children>
</AnchorPane>
  • 写回答

2条回答 默认 最新

  • FireworkRocket 2023-12-23 16:44
    关注

    已解决:
    Main类

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
            Parent root = loader.load();
            Controller controller = loader.getController(); // 获取控制器实例
            // 显示场景和舞台
            Scene scene = new Scene(root, 200, 200);
            primaryStage.setScene(scene);
            primaryStage.show();
            // 在其他类中调用方法来更改Label内容
            OtherClass otherClass = new OtherClass();
            otherClass.changeLabelContent(controller);
        }
    }
    

    OtherClass类

    public class OtherClass {
        public void changeLabelContent(Controller controller) {
            controller.globalLabel.setText("新的内容"); // 修改全局Label的内容
        }
    }
    

    控制器

    import javafx.fxml.FXML;
    import javafx.scene.control.Label;
    
    public class Controller {
        @FXML
        public Label globalLabel; // 全局的JavaFXLabel变量
    
        // 其他方法...
    }
    

    FXML文件

    <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="YourController">
       <children>
          <Label fx:id="globalLabel" text="初始内容" />
       </children>
    </AnchorPane>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 已采纳回答 12月23日
  • 创建了问题 12月23日

悬赏问题

  • ¥15 微信小程序 前端页面内容搜索
  • ¥15 cpu是如何判断当前指令已经执行完毕,然后去执行下条指令的
  • ¥15 C++Codeinject远线程注入
  • ¥15 安装visual studio2022时visualstudiosetup启动不了,闪退。问题代号0x0和0x1389
  • ¥30 java spring boot2.5.3版本websocket连不上
  • ¥15 angular js调外部链接查看pdf
  • ¥15 openFOAM DPMFoam
  • ¥15 将查询到的值,赋值到table指定行中
  • ¥50 docker容器内部启动shell脚本多命令
  • ¥15 请问python的selenium怎么设置referer