JavaFx實現(xiàn)登錄成功跳轉(zhuǎn)到程序主頁面
本文實例為大家分享了JavaFx實現(xiàn)登錄成功跳轉(zhuǎn)到程序主頁面的具體代碼,供大家參考,具體內(nèi)容如下
1、需求
登錄頁面在輸入賬號密碼之后,驗證賬號密碼時候正確,正確就跳轉(zhuǎn)到應(yīng)用程序的首頁。
文筆不行,可能沒怎么寫清楚,歡迎在下面討論
效果如下圖:
2、實現(xiàn)
1)、LoginApplication為啟動類,啟動之后進入到登錄頁面
public class LoginApplication extends Application { ? ? @Override ? ? public void start(Stage stage) throws IOException { ? ? ? ? AnchorPane Login = FXMLLoader.load(getClass().getClassLoader().getResource("FXML/Login/Login.fxml")); ? ? ? ? Scene login = new Scene(Login); ? ? ? ? stage.setTitle("登錄");//設(shè)置標題 ? ? ? ? stage.setScene(login); ? ? ? ? stage.show(); ? ? ? } ? ? ? public static void main(String[] args) { ? ? ? ? launch(args); ? ? } }
2)、LoginController 類進行判斷密碼的正確性,當密碼正確的時候關(guān)閉登錄窗口,打開主頁面窗口
public class LoginController { ? ? @FXML ? ? private TextField NumberTextField; ? ? @FXML ? ? private TextField PasswordTextField; ? ? @FXML ? ? private Label MessageLabel; ? ? @FXML ? ? private Button LoginButton; ? ? public void loginButtonClick(ActionEvent event) throws IOException { ? ? ? ? String number = NumberTextField.getText(); ? ? ? ? String password = PasswordTextField.getText(); ? ? ? ? if (number != null && !number.equals("") && password != null && !password.equals("")) { ? ? ? ? ? ? boolean login = LoginJudge.Login(number, password); ? ? ? ? ? ? if (login == true){ ? ? ? ? ? ? ? ? MessageLabel.setText("登錄成功"); ? ? ? ? ? ? ? ? Stage primaryStage=(Stage)LoginButton.getScene().getWindow();//將submit(登錄按鈕)與MainApplication類中的primaryStage(新窗口)綁定 并執(zhí)行close() ? ? ? ? ? ? ? ? primaryStage.close();//打開新的窗口 所以要關(guān)閉當前的窗口 ? ? ? ? ? ? ? ? MainApplication mainApplication = new MainApplication();//新窗口類 ? ? ? ? ? ? ? ? mainApplication.MainApp();//打開新窗口 ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? MessageLabel.setText("賬號或密碼錯誤"); ? ? ? ? ? ? } ? ? ? ? }else { ? ? ? ? ? ? MessageLabel.setText("請輸入賬號或密碼"); ? ? ? ? } ? ? } }
3)、MainApplication為主頁面的類,LoginController調(diào)用這個類來啟動主頁面
public class MainApplication { ? ? public void MainApp() throws IOException { ? ? ? ? AnchorPane root = FXMLLoader.load(getClass().getClassLoader().getResource("FXML/Main.fxml")); ? ? ? ? Scene scene = new Scene(root); ? ? ? ? scene.getStylesheets().add(getClass().getResource("/CSS/MainCss.css").toExternalForm()); ? ? ? ? Stage stage = new Stage(); ? ? ? ? stage.setTitle("技術(shù)支持工作臺");//設(shè)置標題 ? ? ? ? stage.setScene(scene); ? ? ? ? stage.show(); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring-AOP自動創(chuàng)建代理之BeanNameAutoProxyCreator實例
這篇文章主要介紹了Spring-AOP自動創(chuàng)建代理之BeanNameAutoProxyCreator實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07spring boot中的properties參數(shù)配置詳解
這篇文章主要介紹了spring boot中的properties參數(shù)配置,需要的朋友可以參考下2017-09-09方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10Java多例Bean的應(yīng)用場景-easyExcel導入
EasyExcel 是一個基于 Java 的簡單、省內(nèi)存的讀寫 Excel 的開源項目。這篇文章主要介紹了用easyExcel導入Java Bean的應(yīng)用場景,感興趣的朋友可以參考閱讀2023-04-04springboot如何獲取相對路徑文件夾下靜態(tài)資源的方法
這篇文章主要介紹了springboot如何獲取相對路徑文件夾下靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05