【javaFX基础】实现图形能够根据窗口大小自动调整位置
一、功能说明
1、使圆形 ci 会根据场景大小自动调整位置
2、X坐标:场景宽度的1/2(水平居中)
3、Y坐标:场景高度的1/8

过程中犯了一个低级错误,在此记录下:
无法从静态上下文中引用非静态问题,需通过控制器实例调用非静态方法,因为静态调用之所以失败,是因为 Java 无法确定要操作哪个具体对象的组件!

修复方法调用:
将 HelloController.circleLocationBind(scene)(静态调用)
改为 controller.circleLocationBind(scene)(实例调用)二、代码实践
主要涉及三个文件,代码如下
1、HelloApplication
package org.example.testsc1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;import java.io.IOException;public class HelloApplication extends Application {@Overridepublic void start( Stage primaryStage) throws IOException {FXMLLoader fxmlLoader =new FXMLLoader();fxmlLoader.setLocation(getClass().getResource( "Sc.fxml"));Parent root=fxmlLoader.load();Scene scene =new Scene(root);HelloController controller =fxmlLoader.getController();controller.circleLocationBind(scene);//实现圆随页面大小变化而位置变化primaryStage.setTitle("Hello World");primaryStage.setScene(scene);primaryStage.show();}public static void main(String[] args){ launch(args);}
}
2、HelloController
package org.example.testsc1;import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.shape.Circle;public class HelloController {/*@FXMLprivate Label welcomeText;@FXMLprotected void onHelloButtonClick() {welcomeText.setText("Welcome to JavaFX Application!");*/@FXMLprivate Button bt1;@FXMLprivate Circle ci;public void circleLocationBind(Scene scene) {ci.centerXProperty().bind(scene.widthProperty().divide(2));ci.centerYProperty().bind(scene.heightProperty().divide(8));}@FXMLvoid btAction(ActionEvent event) {System.out.println("您点击了hello按钮");System.out.println("JavaFX版本: " + System.getProperty("javafx.version"));System.out.println("Java版本: " + System.getProperty("java.version"));}public void initialize(){System.out.println("JavaFX版本: " + System.getProperty("javafx.version"));System.out.println("Java版本: " + System.getProperty("java.version"));}
}
3、Sc.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="328.0" prefWidth="480.0" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.testsc1.HelloController"><children><Button fx:id="bt1" layoutX="155.0" layoutY="214.0" mnemonicParsing="false" onAction="#btAction" prefHeight="47.0" prefWidth="106.0" text="Button" /><Label contentDisplay="CENTER" layoutX="155.0" layoutY="136.0" prefHeight="41.0" prefWidth="106.0" text="hello world" textAlignment="CENTER"><font><Font size="15.0" /></font></Label><Circle fx:id="ci" centerX="250.0" centerY="50.0" fill="DODGERBLUE" radius="42.0" stroke="BLACK" strokeType="INSIDE" /></children>
</AnchorPane>
