JavaFX通过“点击”加载新窗口

本文介绍如何在JavaFX中通过点击事件来加载新的窗口。主要讲解了Controller.java中的关键代码,展示了Main.java、main.fxml和sample.fxml的结构,并提供了运行结果的演示。
摘要由CSDN通过智能技术生成

Preface

方法:初始的stage方法一样,没有任何的区别

此处,为了部分(偷懒的)同学方便,给出实例,实则非常简单。

核心的内容是在Controller.java中

    @FXML
    private void newButtonOnClicked(){
        try {
            //一定需要使用try-catch,不然编译器不会让你过的,Trust me!
            Parent anotherRoot = FXMLLoader.load(getClass().getResource("sample.fxml"));
            Stage anotherStage = new Stage();
            anotherStage.setTitle("Another Window Triggered by Clicking");
            anotherStage.setScene(new Scene(anotherRoot, 600, 329));
            anotherStage.show();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

代码

代码结构

sample
├── Controller.java
├── main.fxml
├── Main.java
└── sample.fxml

Main.java

package sample;

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 {
   

    @Override
    
JavaFX提供了一种简单的方法来自定义窗口样式,可以使用CSS样式表来改变窗口的外观。以下是步骤: 1. 创建一个的CSS文件,例如“custom.css”。 2. 在CSS文件中定义你想要的样式,例如: ```css .root { -fx-background-color: #333; } .title-bar { -fx-background-color: #444; -fx-text-fill: white; } .close-button { -fx-background-color: #f00; -fx-text-fill: white; } ``` 3. 在JavaFX应用程序的启动方法中加载CSS文件,并将样式应用到窗口。 ```java public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Custom Window Style"); // Load custom CSS style Scene scene = new Scene(root); scene.getStylesheets().add(getClass().getResource("custom.css").toExternalForm()); // Apply custom style to window primaryStage.initStyle(StageStyle.UNDECORATED); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 4. 在FXML文件中添加一个带有“title-bar”类的Pane,以模拟窗口标题栏。 ```xml <Pane id="title-bar" styleClass="title-bar" onMousePressed="#handleMousePressed" onMouseDragged="#handleMouseDragged"> <Label text="Custom Window Style" /> <Button id="close-button" styleClass="close-button" text="X" onMouseClicked="#handleCloseClicked" /> </Pane> ``` 5. 在控制器类中添加处理鼠标事件的方法,使窗口可以拖动和关闭。 ```java public class Controller { @FXML private void handleMousePressed(MouseEvent event) { xOffset = event.getSceneX(); yOffset = event.getSceneY(); } @FXML private void handleMouseDragged(MouseEvent event) { Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); stage.setX(event.getScreenX() - xOffset); stage.setY(event.getScreenY() - yOffset); } @FXML private void handleCloseClicked(MouseEvent event) { Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); stage.close(); } } ``` 这样,你就可以使用CSS样式表来创建自定义窗口样式了。注意,需要自己添加拖动和关闭窗口的代码逻辑。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值