JAVA fx - controller - 이벤트 메서드 밖으로 코드 빼내기 (2022-08-22)

2022. 8. 22. 22:273층 1구역 - 개발의 장/JAVAfx - GUI

1. 본론

 

Quiz3.java

 

package ex2;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.stage.Stage;

public class Quiz3 extends Application{
	@Override
	public void start(Stage primaryStage) throws Exception {
		FXMLLoader loader = new FXMLLoader(getClass().getResource("quiz3.fxml"));
		
		Parent form = loader.load();
		
		Scene scene = new Scene(form);
		primaryStage.setScene(scene);
		primaryStage.setTitle("quiz3");
		primaryStage.show();
	}

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

}

 

Quiz3_controller.java

 

package ex2;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;

public class Quiz3_controller implements Initializable{
	@FXML private Button loginButton;
	@FXML private TextField idField ;
	@FXML private PasswordField pwField ;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {}
	
	public boolean idLengthCheck() {
		if(idField.getText().length() > 10) {
			return true;
		}
		return false;
	}
	
	public void pwLengthCheck() {
		String tmp = pwField.getText().substring(0, 10);
		pwField.setText(tmp);
	}
	
	public void message(String content) {
		Alert alert = new Alert(AlertType.INFORMATION);
		alert.setContentText(content);
		alert.show();
	}
	
	public void emptyCheck() {
		boolean id = idField.getText().isEmpty();
		boolean pw = pwField.getText().isEmpty();
		if(id || pw) {
			message("아이디 또는 비밀번호를 입력하세요.");
			if(id)
				idField.requestFocus();
			else 
				pwField.requestFocus();
			return;
		}
		loginCheck();
	}
	public void loginCheck() {
		String dbId = "admin", dbPw = "1234";
		if(dbId.equals(idField.getText()) && dbPw.equals(pwField.getText())) {
			message("로그인 성공");
		}else {
			message("로그인 실패");
			idField.clear(); pwField.clear();
			idField.requestFocus();
		}
	}
	
	// 로그인 버튼 클릭하면 호출되는 메서드
	public void loginButtonMethod() {
		if(idLengthCheck()) {
			message("아이디는 10자리 이하로 입력하세요.");
			return;
		}
		
		if(pwField.getText().length() > 10) {
			pwLengthCheck();
			System.out.println("pw : " + pwField.getText());
		}
		
		emptyCheck();
	}
}

 

Quiz3.fxml

 

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<HBox alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ex2.Quiz3_controller">
   <children>
      <VBox alignment="CENTER_RIGHT" prefHeight="200.0" prefWidth="177.0">
         <children>
            <TextField fx:id="idField" maxWidth="150.0" prefHeight="26.0" prefWidth="168.0" />
            <PasswordField fx:id="pwField" maxWidth="150.0" />
         </children>
      </VBox>
      <Button fx:id="loginButton" mnemonicParsing="false" onAction="#loginButtonMethod" prefHeight="50.0" prefWidth="100.0" text="Login" />
   </children>
</HBox>