控制不解析为 Java FX 程序中的 main



我是java的新手。我为动作侦听器编写的下面的 onButtonClick(( 方法总是在 else 块中执行语句。我需要它来验证用户名和密码并执行 if-else 块。奇怪的是,当我在每个方法中添加一些 println 时,我注意到控件根本没有解析为 main。启动方法中的 print 语句在我启动程序后立即执行,而不是 Main 中的第一个 Print 语句。并且主要的两个打印语句根本没有被执行。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.Button;
public class Version3 extends Application {
public static void main(String args) {
System.out.println("main called"); //this is not executing
launch(args);
System.out.println("Finished"); // this aswell
}
Button signInButton;
Button cancelButton;
Text userNameText;
Text passwordText;
Text Validation;
TextField userNameField;
PasswordField passwordField;
//Using gridpane and intialized all Fields above
public void start(Stage primaryStage) {
System.out.println("Stage called"); 
//added a statement to check when this method was calling. Statement 
//getting executed as soon as the program started.
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setVgap(20);
pane.setHgap(20);
userNameText = new Text("Username");
pane.add(userNameText, 0, 0);
userNameField = new TextField();
pane.add(userNameField, 1, 0);
passwordText = new Text("Password");
pane.add(passwordText, 0, 1);
passwordField = new PasswordField();
pane.add(passwordField, 1, 1);
signInButton = new Button("Submit");
pane.add(signInButton, 0, 2);
signInButton.setOnAction(e -> onButtonClick());
// calling method for signin button
Validation = onButtonClick();
pane.add(Validation, 0, 3);
cancelButton = new Button("Clear");
pane.add(cancelButton, 1, 2);
Scene scene = new Scene(pane, 900, 600);
primaryStage.setScene(scene);
primaryStage.setTitle("The Click me app");
primaryStage.show();
}
public Text onButtonClick() {
System.out.println("onButton called");
if (userNameField.getText().equals("admin") && 
passwordField.getText().equals("password")) {
Text val = new Text("Credentials Validated");
//this is neither getting validated nor executing
return val;
} else {
Text val = new Text("Invalid Credentials");
// Always displaying this else block on layout
return val;
}
}
}
  1. 首先,你的main方法应该是这样的

    public static void main(String[] args){
    System.out.println("main called");
    launch(args);
    System.out.println("Finished");
    }
    
  2. 二、更改start(Stage primaryStage)方法中的语句 从

    Validation = onButtonClick();
    

    Validation = new Text("Invalid Credentials");
    
  3. 第三,将onButtonClick()方法更改为

    public void onButtonClick(){
    System.out.println("onButton called");
    if(userNameField.getText().equals("admin") && passwordField.getText().equals("password")){
    Validation.setText("Credentials Validated");
    }else{
    Validation.setText("Invalid Credentials");
    } 
    }
    

main方法中的args参数必须是数组:

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

相关内容

最新更新