javaFx2.x 登陆及其切换界面

本文介绍如何使用JavaFX 2.x创建登录界面,并在登录成功后切换到新的界面。示例代码包括了界面元素布局、事件处理以及简单的数据库查询验证登录。开发工具为Eclipse和e(r)xclipse插件,JDBC用于数据库连接。

最近对javaFx特别感兴趣,闲暇时对着官方文档及GOOGLE学习

没有太详细的解说,但在代码中有还算较明白的注释


示例使用JDK7,开发工具为eclipse和e(rx)clipse插件

效果如下图





下面是代码


程序的入口

LoginWidthCss.class

  1. package com.wei.table;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import com.wei.jdbc.JdbcUtil;
  7. import javafx.application.Application;
  8. import javafx.event.ActionEvent;
  9. import javafx.event.EventHandler;
  10. import javafx.geometry.Insets;
  11. import javafx.geometry.Pos;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.PasswordField;
  16. import javafx.scene.control.TextField;
  17. import javafx.scene.layout.GridPane;
  18. import javafx.scene.layout.HBox;
  19. import javafx.scene.text.Text;
  20. import javafx.stage.Stage;
  21. /**
  22. * 登陆首页面
  23. * @author wangwei
  24. *
  25. */
  26. public class LoginWidthCss extends Application {
  27. private Connection conn;
  28. private PreparedStatement ps;
  29. private ResultSet rs;
  30. private GridPane grid;
  31. public static void main(String[] args) {
  32. launch(args );
  33. }
  34. @Override
  35. public void start(final Stage primaryStage) throws Exception {
  36. //窗口标题
  37. primaryStage.setTitle("JavaFX 登陆");
  38. //设置面板及布局
  39. grid = new GridPane();
  40. grid.setAlignment(Pos.CENTER);
  41. grid.setHgap(10);
  42. grid.setVgap(10);
  43. grid.setPadding(new Insets(25));
  44. //文本框
  45. Text scenetitle = new Text("Welcome");
  46. scenetitle.setId("welcome-text");
  47. grid.add(scenetitle, 0, 0, 2, 1);
  48. //标签
  49. Label userName = new Label("用户名:");
  50. grid.add(userName, 0, 1);
  51. //文本输入框
  52. final TextField userTextField = new TextField();
  53. grid.add(userTextField, 1, 1);
  54. Label passwd = new Label("密码");
  55. grid.add(passwd, 0, 2);
  56. //密码输入框
  57. final PasswordField passwdField = new PasswordField();
  58. grid.add(passwdField, 1, 2);
  59. //按钮及按钮布局
  60. Button btn = new Button("登陆");
  61. HBox hbBtn = new HBox(10);
  62. hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
  63. hbBtn.getChildren().add(btn);
  64. grid.add(hbBtn, 1, 4);
  65. //点击登陆按钮显示的提示文字
  66. final Text actiontarget = new Text();
  67. actiontarget.setId("actiontarget");
  68. grid.add(actiontarget, 1, 6);
  69. //登陆按钮单击事件
  70. btn.setOnAction(new EventHandler<ActionEvent>() {
  71. @Override
  72. public void handle(ActionEvent arg0) {
  73. actiontarget.setText("登陆成功");
  74. grid.setVisible(false);
  75. //表格面板
  76. TablePane tp = new TablePane();
  77. //加到场景中
  78. Scene tpScene = new Scene(tp,500,500);
  79. //切换舞台场景为表格面板
  80. primaryStage.setScene(tpScene);
  81. /* //获取用户名,密码的值
  82. String userName = userTextField.getText();
  83. String passWord = passwdField.getText();
  84. //建立连接查询数据库
  85. conn = JdbcUtil.getConn();
  86. String sql = "select t.*, t.rowid from itcsys_user t where t.usercode='"+userName+"' and t.username='"+passWord+"'";
  87. try {
  88. ps = conn.prepareStatement(sql);
  89. rs = ps.executeQuery();
  90. if(rs.next()){
  91. actiontarget.setText("登陆成功");
  92. grid.setVisible(false);
  93. //表格面板
  94. TablePane tp = new TablePane();
  95. //加到场景中
  96. Scene tpScene = new Scene(tp,500,500);
  97. //切换舞台场景为表格面板
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值