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

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
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Controller.java(核心代码处)

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Controller {

    @FXML
    private Button newButton;

    @FXML
    private void newButtonOnClicked(){
        try {
            //一定需要使用try-catch
            //下段实现的内容和Main.java中其实是一样的
            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();
        }

    }

}

main.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.text.Font?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="newButton" layoutX="310.0" layoutY="214.0" mnemonicParsing="false" onMouseClicked="#newButtonOnClicked" text="New FXML" />
      <Label alignment="CENTER" layoutX="142.0" layoutY="115.0" prefHeight="51.0" prefWidth="70.0" text="Main" textAlignment="CENTER">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

sample.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <AnchorPane prefHeight="329.0" prefWidth="369.0" GridPane.columnIndex="1">
         <children>
            <Button layoutX="201.0" layoutY="180.0" mnemonicParsing="false" text="Test" />
         </children>
      </AnchorPane>
   </children>
</GridPane>

运行结果演示

triggered by clicking

没了

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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、付费专栏及课程。

余额充值