java fx 基本使用
1. 窗体:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
//从classpath下开始查找fxml文件,如果不以/开头,则是从当前类路径下加载
Parent root = FXMLLoader.load(getClass().getResource("/comparetor.fxml"));
primaryStage.setTitle("标题");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.getIcons().add(new Image(
Main.class.getResourceAsStream("/logo.png")));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
2. Controller绑定
在fxml文件的布局属性中
fx:controller="xx.controller"
3. CSS绑定
在fxml文件的布局属性中
stylesheets="@css/xx.css"
4. 事件绑定
在Controller中,
@FXML
private Buttion buttion;//button为fxml中的元素id
Maven项目类型下的依赖
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
打包为可执行jar
<build>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>启动类全限定名称</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
资料:
java fx 中文文档 - 中文文档 | JavaFX中文资料
https://blog.csdn.net/weixin_42363733/article/details/80569820 (JavaFX窗口最大化及全屏相关设置)