Javafx按钮不做任何事情



我正在使用eclipse, JavaFX和场景构建器创建一个简单的登录GUI表单。我已经编码了我的程序,所以每次我点击登录按钮,它会切换到另一个窗口,但它不工作。我是java的初学者,但我们的任务是在我的课上创建一个系统,所以我只依赖于youtube教程,任何帮助都会很感激!

这是我的主代码

package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

my login form

package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Login {
    @FXML
    private Button button;
    @FXML
    private PasswordField password;
    @FXML
    private TextField username;
    @FXML
    private Label wrongLogin;
    @FXML
    public void Login(ActionEvent event) {
        
        Stage primaryStage = new Stage();
        
        if (username.getText().equals("admin") && password.getText().equals("admin")) {
            try {
                Parent root = FXMLLoader.load(getClass().getResource("BookRooms.fxml"));
                Scene scene = new Scene(root);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

my FXML file

<?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.layout.AnchorPane?>
<AnchorPane prefHeight="345.0" prefWidth="545.0" style="-fx-background-color: #fff2cc;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Login">
   <children>
      <AnchorPane layoutX="132.0" layoutY="62.0" prefHeight="221.0" prefWidth="279.0" style="-fx-background-color: #545454; -fx-border-radius: 10;">
         <children>
            <Label layoutX="34.0" layoutY="60.0" prefHeight="25.0" prefWidth="56.0" text="Username:" textFill="WHITE" />
            <Label layoutX="34.0" layoutY="98.0" prefHeight="25.0" prefWidth="56.0" text="Password:" textFill="WHITE" />
            <TextField layoutX="106.0" layoutY="60.0" promptText="Enter Username ID:" />
            <TextField layoutX="106.0" layoutY="98.0" promptText="Enter Password" />
            <Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />
         </children></AnchorPane>
      <Label layoutX="225.0" layoutY="36.0" text="HOTEL DEL LUNA" />
   </children>
</AnchorPane>

fxml中的按钮不能在其控制器类中调用Login(),因为fxml按钮标签中没有onAction属性。
因此,在<Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />中添加onAction="#Login"在按钮标签中:

<Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" onAction="#Login" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />

并且,在类中使用与该类完全相同的名称来命名方法被认为是一种不好的做法。只有构造函数必须将其名称与类标识符

匹配

最新更新