Javafx fxml动态场景过渡

我正在使用基于Intellij的JavaFX的图书馆管理系统,但是在跨阶段过渡和在同一阶段上载时遇到了麻烦。

Main.java

import javafx.application.Application;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {
    private double x,y;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(Main.class.getResource("gui/Login.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.initStyle(StageStyle.UNDECORATED);

        //we gonna drag the frame
        root.setOnmousepressed(event -> {
            x = event.getSceneX();
            y = event.getSceneY();
        });

        root.setOnmouseDragged(event -> {
            primaryStage.setX(event.getScreenX() - x);
            primaryStage.setY(event.getScreenY() - y);
        });
        primaryStage.show();
    }

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

Login.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="657.0" prefWidth="1063.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.LoginController">
   <children>
      <Label alignment="CENTER" layoutX="888.0" layoutY="378.0" prefHeight="20.0" prefWidth="21.0" text="X" textFill="#ddd9d9">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <Pane fx:id="toppane" layoutX="-6.0" layoutY="-12.0" prefHeight="117.0" prefWidth="1072.0" style="-fx-background-color: #CE201C;">
         <children>
            <ImageView fitHeight="81.0" fitWidth="265.0" layoutX="11.0" layoutY="25.0">
               <image>
                  <Image url="@../images/logo.png" />
               </image>
            </ImageView>
            <Pane layoutX="360.0" layoutY="54.0" prefHeight="51.0" prefWidth="320.0" style="-fx-border-color: #000000;">
               <children>
                  <Text fill="#fcbb31" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Login" textAlignment="CENTER" wrappingWidth="321.1429138183594">
                     <font>
                        <Font size="28.0" />
                     </font>
                  </Text>
               </children>
            </Pane>
            <Button fx:id="closeButton" layoutX="1033.0" layoutY="13.0" mnemonicParsing="false" onaction="#handlecloseButtonaction" prefHeight="13.0" prefWidth="25.0" style="-fx-background-color: #FCBB31;" text="X" textAlignment="CENTER" textFill="WHITE" />
         </children>
      </Pane>
      <Pane fx:id="bottompane" layoutX="-6.0" layoutY="619.0" prefHeight="38.0" prefWidth="1072.0" style="-fx-background-color: #CE201C;">
         <children>
            <Text fill="#fcbb31" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="● Database connected" textAlignment="CENTER" wrappingWidth="206.47628784179688">
               <font>
                  <Font size="16.0" />
               </font>
            </Text>
            <Button layoutX="946.0" layoutY="7.0" mnemonicParsing="false" prefHeight="15.0" prefWidth="112.0" style="-fx-background-color: #FCBB31;" text="Database Settings" textAlignment="CENTER" textFill="WHITE" />
         </children>
      </Pane>
      <TextField fx:id="loginusername" layoutX="435.0" layoutY="178.0" prefHeight="34.0" prefWidth="194.0" promptText="username" />
      <TextField fx:id="loginpassword" layoutX="435.0" layoutY="248.0" prefHeight="34.0" prefWidth="194.0" promptText="Password" />
      <Button fx:id="loginbutton" layoutX="320.0" layoutY="329.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Login" textFill="WHITE" />
      <Button fx:id="forgotbutton" layoutX="590.0" layoutY="329.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Forgot Password?" textFill="WHITE" />
      <Text fill="#ce201c" layoutX="505.0" layoutY="606.0" strokeType="OUTSIDE" strokeWidth="0.0" text="● If you want to register to the library,you have to contact with any librarian." textAlignment="CENTER" wrappingWidth="543.4762878417969">
         <font>
            <Font size="16.0" />
         </font>
      </Text>
   </children>
</AnchorPane>

LoginController.java

package gui;

import javafx.event.actionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class LoginController {

    @FXML
    private Pane toppane;

    @FXML
    private Button closeButton;

    @FXML
    private Pane bottompane;

    @FXML
    private Button dbsettingsbutton;

    @FXML
    private TextField loginusername;

    @FXML
    private TextField loginpassword;

    @FXML
    private Button loginbutton;

    @FXML
    private Button forgotbutton;

    @FXML
    void handlecloseButtonaction(actionEvent event) {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }

}

librarian.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="657.0" prefWidth="1063.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.librarianController">
   <children>
      <Pane fx:id="mainpane" layoutX="11.0" layoutY="111.0" prefHeight="502.0" prefWidth="167.0" style="-fx-border-color: #000000;">
         <children>
            <Pane layoutX="7.0" layoutY="7.0" prefHeight="38.0" prefWidth="150.0" style="-fx-border-color: #000000;">
               <children>
                  <Text fx:id="options" fill="#ce201c" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Options" textAlignment="CENTER" wrappingWidth="150.0">
                     <font>
                        <Font name="System Bold" size="18.0" />
                     </font>
                  </Text>
               </children>
            </Pane>
            <Button fx:id="mstudentbutton" layoutX="7.0" layoutY="52.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Manage Student" textFill="WHITE" />
            <Button fx:id="mbookbutton" layoutX="7.0" layoutY="103.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Manage Book" textFill="WHITE" />
            <Button fx:id="sbookbutton" layoutX="7.0" layoutY="154.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Search Book" textFill="WHITE" />
            <Button fx:id="mborrowerbutton" layoutX="7.0" layoutY="205.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Manage Borrower" textFill="WHITE" />
            <Button fx:id="brequestbutton" layoutX="7.0" layoutY="256.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Book Request" textFill="WHITE" />
            <Button fx:id="ubookbutton" layoutX="7.0" layoutY="307.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Upload Book" textFill="WHITE" />
            <Button fx:id="nboardbutton" layoutX="7.0" layoutY="358.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Notice Board" textFill="WHITE" />
            <Button fx:id="eprofilebutton" layoutX="7.0" layoutY="409.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Edit Profile" textFill="WHITE" />
            <Button fx:id="cpasswordbutton" layoutX="7.0" layoutY="460.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="150.0" style="-fx-background-color: #CE201C;" text="Change Password" textFill="WHITE" />
         </children>
      </Pane>
      <Label alignment="CENTER" layoutX="888.0" layoutY="378.0" prefHeight="20.0" prefWidth="21.0" text="X" textFill="#ddd9d9">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <ButtonBar layoutX="683.0" layoutY="195.0" prefHeight="40.0" prefWidth="200.0" />
      <Pane fx:id="toppane" layoutX="-6.0" layoutY="-12.0" prefHeight="117.0" prefWidth="1072.0" style="-fx-background-color: #CE201C;">
         <children>
            <ImageView fitHeight="81.0" fitWidth="265.0" layoutX="11.0" layoutY="25.0">
               <image>
                  <Image url="@../images/logo.png" />
               </image>
            </ImageView>
            <Button layoutX="823.0" layoutY="70.0" mnemonicParsing="false" prefHeight="15.0" prefWidth="112.0" style="-fx-background-color: #FCBB31;" text="Home" textAlignment="CENTER" textFill="WHITE">
               <font>
                  <Font size="16.0" />
               </font>
            </Button>
            <Button layoutX="946.0" layoutY="70.0" mnemonicParsing="false" prefHeight="15.0" prefWidth="112.0" style="-fx-background-color: #FCBB31;" text="Log Out" textAlignment="CENTER" textFill="WHITE">
               <font>
                  <Font size="16.0" />
               </font>
            </Button>
            <Pane layoutX="360.0" layoutY="54.0" prefHeight="51.0" prefWidth="320.0" style="-fx-border-color: #000000;">
               <children>
                  <Text fill="#fcbb31" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Borrow Book" textAlignment="CENTER" wrappingWidth="321.1429138183594">
                     <font>
                        <Font size="28.0" />
                     </font>
                  </Text>
               </children>
            </Pane>
            <Button fx:id="closeButton" layoutX="1033.0" layoutY="13.0" mnemonicParsing="false" onaction="#handlecloseButtonaction" prefHeight="13.0" prefWidth="25.0" style="-fx-background-color: #FCBB31;" text="X" textAlignment="CENTER" textFill="WHITE" />
         </children>
      </Pane>
      <Pane fx:id="bottompane" layoutX="-6.0" layoutY="619.0" prefHeight="38.0" prefWidth="1072.0" style="-fx-background-color: #CE201C;">
         <children>
            <Text fill="#fcbb31" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="● Database connected" textAlignment="CENTER" wrappingWidth="206.47628784179688">
               <font>
                  <Font size="16.0" />
               </font>
            </Text>
            <ColorPicker fx:id="colorpicker" layoutX="933.0" layoutY="7.0" onaction="#changeColor" />
         </children>
      </Pane>
   </children>
</AnchorPane>

librarian.java

package gui;

import javafx.event.actionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.awt.*;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.scene.control.ColorPicker;
import javafx.scene.layout.Pane;

public class librarianController implements Initializable
{
    @FXML
    private Pane mainpane;

    @FXML
    private Text options;

    @FXML
    private Button mstudentbutton;

    @FXML
    private Button mbookbutton;

    @FXML
    private Button sbookbutton;

    @FXML
    private Button mborrowerbutton;

    @FXML
    private Button brequestbutton;

    @FXML
    private Button ubookbutton;

    @FXML
    private Button nboardbutton;

    @FXML
    private Button eprofilebutton;

    @FXML
    private Button cpasswordbutton;

    @FXML
    private Pane toppane;

    @FXML
    private Button closeButton;

    @FXML
    private Pane bottompane;

    @FXML
    private ColorPicker colorpicker;

    @FXML
    void changeColor(actionEvent event) {
        toppane.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),CornerRadii.EMPTY,Insets.EMPTY)));
        bottompane.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        options.setfill(Paint.valueOf(colorpicker.getvalue().toString()));
        mstudentbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        mbookbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        sbookbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        mborrowerbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        brequestbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        ubookbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        nboardbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        eprofilebutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
        cpasswordbutton.setBackground(new Background(new BackgroundFill(Paint.valueOf(colorpicker.getvalue().toString()),Insets.EMPTY)));
    }

    @FXML
    void handlecloseButtonaction(actionEvent event) {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }

    @Override
    public void initialize(URL url,ResourceBundle resourceBundle) {

    }
}

当我登录系统时,我想到现场转为图书馆员。但是,如果只更改场景内容并且上下两侧保持不变,那还可以吗?另外,单击面板空白部分右侧的按钮即可。

登录场景:

Javafx fxml动态场景过渡

图书管理员场景:

Javafx fxml动态场景过渡

zsqforsap 回答:Javafx fxml动态场景过渡

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2969032.html

大家都在问