[英]JavaFX Login with FXML
I did a simple login window with Eclipse (Main.java, application.css, Login.fxml) How can I add a listener on the 2 buttons? 我使用Eclipse(Main.java,application.css,Login.fxml)做了一个简单的登录窗口,如何在两个按钮上添加侦听器?
Main.java Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root,350,150);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("LOGIN");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Login.fxml Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.layout.HBox?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" hgap="10" vgap="10">
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
<children>
<Label text="Username:" GridPane.columnIndex="0"
GridPane.rowIndex="0" GridPane.halignment="RIGHT" />
<Label text="Password:" GridPane.columnIndex="0"
GridPane.rowIndex="1" GridPane.halignment="RIGHT" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<PasswordField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<HBox GridPane.columnIndex="0" GridPane.rowIndex="2"
GridPane.columnSpan="2" alignment="CENTER" spacing="10">
<children>
<Button text="Login" />
<Button text="Annulla" />
</children>
</HBox>
</children>
</GridPane>
application.css application.css
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
.label {
-fx-text-fill: rgb(255,0,0);
-fx-font-weight: bold;
-fx-font-style: italic;
}
I need to get username e password and connect to a database 我需要获取用户名和密码并连接到数据库
You need to define a controller class . 您需要定义一个控制器类 。
You can inject the controls into the controller instance by annotating them @FXML
, and you can associate methods in the controller with handlers for the buttons: 您可以通过为控件添加
@FXML
注释来将控件注入到控制器实例中,并且可以将控制器中的方法与按钮的处理程序相关联:
package application ;
public class LoginController {
@FXML
private TextField userIdField ;
@FXML
private PasswordField passwordField ;
@FXML
private void login() {
String userId = userIdField.getText();
String password = passwordField.getText();
// etc...
}
}
In your FXML, use fx:controller
on the root element to specify the class to be used to create a controller. 在FXML中,在根元素上使用
fx:controller
来指定用于创建控制器的类。 Use fx:id
to inject an element into the controller, and use onAction="#methodName"
to specify a handler method for the action on a button: 使用
fx:id
将元素注入到控制器中,并使用onAction="#methodName"
为按钮上的操作指定处理程序方法:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.layout.HBox?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" hgap="10" vgap="10"
fx:controller="application.LoginController">
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
<children>
<Label text="Username:" GridPane.columnIndex="0"
GridPane.rowIndex="0" GridPane.halignment="RIGHT" />
<Label text="Password:" GridPane.columnIndex="0"
GridPane.rowIndex="1" GridPane.halignment="RIGHT" />
<TextField fx:id="userIdField" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<HBox GridPane.columnIndex="0" GridPane.rowIndex="2"
GridPane.columnSpan="2" alignment="CENTER" spacing="10">
<children>
<Button text="Login" onAction="#login" />
<Button text="Annulla" />
</children>
</HBox>
</children>
</GridPane>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.