gpt4 book ai didi

java - 控制不解析到javafx程序中的main

转载 作者:行者123 更新时间:2023-12-01 17:55:22 24 4
gpt4 key购买 nike

我是java新手。我为 Action 监听器编写的下面的onButtonClick()方法始终执行else block 中的语句。我需要它来验证用户名和密码并执行if-else block 。奇怪的是,当我在每个方法中添加一些 println 时,我注意到控件根本没有解析到 main。当我启动程序时,start 方法中的打印语句首先执行,而不是 main 中的第一个打印语句。 main 中的两个打印语句根本没有被执行。

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");
    }
    }

关于java - 控制不解析到javafx程序中的main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45429397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com