javafx之登陆界面的跳转

界面布局用到的是fxml而非纯java代码,工具是javafx sence builder

javafx之登陆界面的跳转_第1张图片

账号:account 密码:password

javafx之登陆界面的跳转_第2张图片

登陆成功:

javafx之登陆界面的跳转_第3张图片

可以点击退出登陆返回到登陆页面


工程目录:

javafx之登陆界面的跳转_第4张图片

package check;

/**
 *
 * @author itwang
 */
public class Check {
    public static boolean checkreturn(String account,String password){
     boolean checkbool = false;
    if("account".equals(account)&&"password".equals(password)){
       checkbool = true;
    }
    return checkbool;
    }
}

package fxmltest;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

/**
 *
 * @author itwang
 */
public class LoginController implements Initializable {
    @FXML private TextField account;
    @FXML private PasswordField password;
     private FXMLTest application;
    
    
    public void setApp(FXMLTest application){
        this.application = application;
    }
    @FXML
    public void LOGIN_M(ActionEvent event) {
        application.userlogin(account.getText(), password.getText());
    }
    
    @FXML
    private void CLEAR_M(ActionEvent event) {
        account.setText(null);
        password.setText(null);
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}

package fxmltest;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

/**
 *
 * @author itwang
 */
public class MainController implements Initializable{
    private FXMLTest application;
    
    public void setApp(FXMLTest application){
        this.application = application;
    }
    
   @FXML
    private void OUT_M(ActionEvent event) {
       application.useroutmain();
    }
   
    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }
    
}

package fxmltest;

import check.Check;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 *
 * @author itwang
 */
public class FXMLTest extends Application {
    private Stage stage;
    private final double MINIMUM_WINDOW_WIDTH = 400.0;
    private final double MINIMUM_WINDOW_HEIGHT = 250.0;
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        stage.setTitle("FXML Login Sample");
        stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
        stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
        gotologin();
        stage.show();
    }
    public void gotologin(){
      try {
            LoginController login = (LoginController) replaceSceneContent("FXML_LOGIN.fxml");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(FXMLTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void gotomain(){
     try {
            MainController main = (MainController) replaceSceneContent("FXML_MAIN.fxml");
            main.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(FXMLTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void userlogin(String account,String password){
    if(Check.checkreturn(account,password)){
      gotomain();
    }
    }
    public void useroutmain(){
      gotologin();
    }
   private Initializable replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = FXMLTest.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(FXMLTest.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        } 
        Scene scene = new Scene(page, 800, 600);
        stage.setScene(scene);
        stage.sizeToScene();
        return (Initializable) loader.getController();
    }
    public static void main(String[] args) {
        launch(args);
    }
}













  
    
      
        
          
            
          
        
        
          
            
            
            
            
            
            
          
          
            
            
          
          
            
            
            
          
        
      
      
        
      
    
  












  
    
      
        
      
    
  




你可能感兴趣的:(javafx)