我正在尝试用 JavaFX 制作我的第一个 GUI。这是我到目前为止所做的:
主要的:
package ZVCVolkel_GUI;
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("LoginScreen.fxml"));
primaryStage.setTitle("Zweegvliegclub Volkel");
primaryStage.setScene(new Scene(root, 322.0, 182.0));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
package ZVCVolkel_GUI;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class Controller {
@FXML
private JFXTextField username;
@FXML
private JFXPasswordField password;
@FXML
private JFXButton login;
@FXML
void makeLogin(ActionEvent event){
String user = username.getText();
String pass = password.getText();
if(user.equals("Admingeb") && pass.equals("Adminww")){
System.out.println("Welcome");
}else{
System.out.println("Access denied");
}
}
}
FXML 文件
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="182.0" prefWidth="322.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ZVCVolkel_GUI.Controller">
<children>
<Label layoutX="79.0" layoutY="14.0" text="Inloggen bij ZVC Volkel">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<JFXButton fx:id="login" blendMode="SRC_ATOP" layoutX="128.0" layoutY="132.0" mnemonicParsing="false" onAction="#makeLogin" text="Inloggen">
<font>
<Font name="System Bold" size="12.0" />
</font>
</JFXButton>
<JFXPasswordField fx:id="password" layoutX="94.0" layoutY="79.0" promptText="Wachtwoord" />
<JFXTextField fx:id="username" layoutX="94.0" layoutY="54.0" promptText="Gebruikersnaam" />
</children>
</AnchorPane>
现在,当我尝试执行它时,我收到一条错误消息,提示我的 JFXButton 上的 onAction '无法解析符号'。当我删除 onAction 时,它工作正常,但我真的需要它才能拥有一个有用的程序。任何人都知道为什么我的 onAction 不起作用?