javafxxml编写登录注册界面
时间: 2024-01-26 18:03:47 浏览: 15
以下是一个基本的JavaFX登录和注册界面的XML代码示例:
```
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<GridPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"
alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10" prefWidth="100"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10" prefWidth="100"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10" prefHeight="30" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10" prefHeight="30" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10" prefHeight="30" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10" prefHeight="30" vgrow="SOMETIMES"/>
</rowConstraints>
<Label text="Username:" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<TextField fx:id="usernameField" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Button text="Log In" onAction="#loginButtonClicked" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Separator GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="3"/>
<Label text="New User? Register Here:" GridPane.columnIndex="0" GridPane.rowIndex="4" GridPane.columnSpan="2"/>
<Label text="Username:" GridPane.columnIndex="0" GridPane.rowIndex="5"/>
<TextField fx:id="newUsernameField" GridPane.columnIndex="1" GridPane.rowIndex="5"/>
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="6"/>
<PasswordField fx:id="newPasswordField" GridPane.columnIndex="1" GridPane.rowIndex="6"/>
<Button text="Register" onAction="#registerButtonClicked" GridPane.columnIndex="1" GridPane.rowIndex="7"/>
</GridPane>
```
这个XML代码定义了一个 GridPane 布局,其中包含了两个文本框(用于输入用户名和密码)、两个密码框(用于输入密码)、两个按钮(用于登录和注册)以及一条分隔线。其中,GridPane 的每个子节点都被分配到相应的行和列,并且可以通过 fx:id 属性与JavaFX的Controller类中的变量进行绑定。在此示例中,我们使用了 “onAction” 属性来指定按钮点击后要执行的方法。