环境:idea,maven创建JavaFX工程
工程目录如下:
MusicPlayer.java
package cn.com;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import static cn.com.value.Values.TITLE;
/**
* @copyright 2003-2024
* @author qiao wei
* @date 2024-10-01
* @version 1.0
* @brief
* @history name
* date
* brief
*/
public class MusicPlayer extends Application{
public MusicPlayer() {}
@Override
public void start(Stage primaryStage) throws IOException {
// Stage primaryStage = new Stage();
/**
* 三种方式加载fxml文件。两种方法都是从工程根目录的资源文件夹(resources)下加载。
*/
// FXMLLoader fxmlLoader = new FXMLLoader(MusicPlayer.class.getResource("/fxml/index.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/index.fxml"));
// FXMLLoader fxmlLoader = new FXMLLoader();
// fxmlLoader.setLocation(getClass().getResource("/fxml/index.fxml"));
AnchorPane rootPane = fxmlLoader.load();
rootPane.setBackground(
new Background(
new BackgroundFill(
Color.RED,
new CornerRadii(1),
new Insets(10)
)
)
);
Scene scene = new Scene(rootPane);
// 根据屏幕设置窗体尺寸。
Rectangle2D rectangle2D = Screen.getPrimary().getBounds();
double width = rectangle2D.getWidth();
double height = rectangle2D.getHeight();
primaryStage.setWidth(width * 3 / 5);
primaryStage.setHeight(height * 3 / 5);
// 窗体只有关闭按钮,没有最小化,最大化按钮。
// primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setTitle(TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
运行结果: