JavaFx+JFoenix創建一個登錄界面
1.環境搭建閱讀下面的博客
JavaFx+JFoenix開發環境搭建+Demo:環境搭建
2. 登錄界面demo
package javafxdemo;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
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.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* @author bruce
*
*/
public class Login extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
//設置窗口名稱
stage.setTitle("Welcome javaFX");
//設置窗口寬度
stage.setWidth(800);
//設置創庫高度
stage.setHeight(800);
//創建一個網格窗格
GridPane gridPane = new GridPane();
//設置網格居中顯示
gridPane.setAlignment(Pos.CENTER);
//設置每個格子之間垂直間距
gridPane.setHgap(10);
//設置每個格子之間水平間距
gridPane.setVgap(10);
//padding屬性管理網格窗格邊緣周圍的空間,按照頂部,右側,底部和左側的順序排列
gridPane.setPadding(new Insets(20,20,20,20));
//創建一個文本
Text scenetitle = new Text("WelCome");
//設置文本字體
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
//將文本添加到網格的第0列0行,列的跨度2,行的跨度1
gridPane.add(scenetitle,0,0,2,1);
//創建一個標籤
Label user_name = new Label("User Name");
//將user_name標籤添加到網格窗口的0列1行
gridPane.add(user_name,0,1);
//創建一個可輸入文本框
TextField usertextField = new TextField();
//將可輸入文本框添加到網格窗口的1列1行
gridPane.add(usertextField,1,1);
//創建一個標籤
Label pass_word = new Label("Pass Word");
//將pass_word標籤添加到網格窗口的0列2行
gridPane.add(pass_word,0,2);
//創建一個password類型的輸入框
PasswordField pwBox = new PasswordField();
//將password類型的輸入框添加到網格的1列2行位置
gridPane.add(pwBox,1,2);
//創建一個按鈕
Button btn = new Button("Sign in");
//創建一個盒子
HBox hBox = new HBox();
//設置盒子對齊方式爲底部右邊
hBox.setAlignment(Pos.BOTTOM_RIGHT);
//將按鈕添加到盒子中
hBox.getChildren().add(btn);
//將盒子添加到網格窗口的1列4行位置
gridPane.add(hBox,1,4);
//創建一個空文本,在下面的事件中調用該文本。
Text actiontarget = new Text();
//將文本添加到網格窗口的1列6行
gridPane.add(actiontarget,1,6);
// 創建一個時間,當點擊登錄按鈕時候觸發上面actiontarget的文本
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
//設置 Test的文本顏色
actiontarget.setFill(Color.FIREBRICK);
//設置 Test的文本內容
actiontarget.setText("Sign in button pressed");
}
});
//設置是否顯網格線,在調試時候網格線可以作爲佈局調整依據。
gridPane.setGridLinesVisible(true);
//1.創建一個場景,將網格佈局放入場景中
Scene scene = new Scene(gridPane, 700, 700);
//2.將場景放入窗口中。
stage.setScene(scene);
//3.打開窗口
stage.show();
}
}