With a TextField and PasswordField, we can enter text.
If the submit button is pressed and the password is correct (password), the person is greeted with green OK.
package ex67;
import javafx.application.Application;
import javafx.geometry.Pos;
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.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Ex67 extends Application {
@Override
public void start(Stage stage) {
Font font = Font.font("Geogia",20);
GridPane gridPane = new GridPane();
gridPane.setHgap(10); gridPane.setVgap(10);
gridPane.setAlignment(Pos.CENTER);
Label nameLab = new Label("Name");
nameLab.setFont(font);
gridPane.add(nameLab, 0, 0);
TextField nameTF = new TextField();
nameTF.setMaxWidth(100);
gridPane.add(nameTF, 1, 0);
Label passwordLab = new Label("Password");
passwordLab.setFont(font);
gridPane.add(passwordLab, 0, 1);
PasswordField passwordField = new PasswordField();
passwordField.setMaxWidth(200);
gridPane.add(passwordField, 1, 1);
Button submit = new Button("Submit");
gridPane.add(submit, 1, 2, 2, 1);
Label messageLab = new Label();
messageLab.setFont(font);
messageLab.setPrefWidth(200);
gridPane.add(messageLab, 1, 3, 2, 1);
Scene scene = new Scene(gridPane, 500, 200);
stage.setTitle("Example 67. TextField and PasswordField");
submit.setOnAction(e -> {
String message;
String password = passwordField.getText();
if (password.equals("password")) {
message = "OK";
messageLab.setTextFill(Color.GREEN);
} else {
message = "Wrong";
messageLab.setTextFill(Color.RED);
}
messageLab.setText(message + ", " + nameTF.getText());
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
This is the output:
No comments:
Post a Comment