Javafx之网格布局,下拉框,单(多)选框

一、连接数据库 帮助类

package com.zking.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 连接数据库 帮助类
 * @author Administrator
 *
 */
public class DBHelper {
	//注册驱动
	static {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (Exception e) {
			e.printStackTrace();//处理异常信息
		}
	}
	
	//连接语句
	private static final String url = "jdbc:sqlserver://sqlserver:1433;DatabaseName=T283";
	//获得连接
	public static Connection getCon() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url,"sa","123");
		} catch (Exception e) {
			e.printStackTrace();//处理异常信息
		}
		return con;
	}
	
	public static void close(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(rs!=null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();//处理异常信息
		}
	}
}

二、实体类 

package com.zking.entity;

/**
 * 学生 实体类
 * 定义属性
 * @author Administrator
 *
 */
public class Student {
	//定义属性,私有化属性
	private int id;
	private String name;
	private String password;
	private String sex;
	//封装
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	//构造函数
	public Student() {
		super();
	}
	public Student(int id, String name, String password, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.sex = sex;
	}
	public Student(String name, String password, int age, String sex) {
		super();
		this.name = name;
		this.password = password;
		this.sex = sex;
	}
	//toString
	public String toString() {
		return "编号 " + id + "\t 姓名 " + name + "\t 密码 " + password + "\t  性别 " + sex;
	}
}

三、Dao类

package com.zking.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.zking.entity.Student;
import com.zking.util.DBHelper;

/**
 * 数据库 登录查询  or 验证
 * 方法
 * @author Administrator
 *
 */
public class StuDao {
//	//定义资源  即 声明对象
//	private Connection con;
//	private PreparedStatement ps;
//	private ResultSet rs;
	
	//传两个参数-用户名和 密码,为了连接实现数据库登录验证(查询)
	public Student login(String name,String pwd) {
		Student s = null;
		//声明对象
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBHelper.getCon();//连接数据库
			String sql = "select * form studentfx where sname = ? and spassword =?";
			ps = con.prepareStatement(sql);//创建对象
			ps.setString(1, name);
			ps.setString(2, pwd);//给占位符赋值
			rs = ps.executeQuery();//执行 SQL语句
			if(rs.next()) {
				//把数据存进 学生对象
				s = new Student(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
				return s;//如果数据库中有此人,返回 学生对象
			}
		} catch (Exception e) {
			e.printStackTrace();//处理异常信息
		}finally {
			DBHelper.close(con, ps, rs);//关闭连接
		}
		return null;//查无此人,返回 null值
	}
}

四、测试类

登录 login

package com.zking.test;

import com.zking.dao.StuDao;
import com.zking.entity.Student;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 登录 login
 * @author Administrator
 *
 */
public class Login extends Application{
	//将元素定义在这个位置,其他地方也可调用
	StuDao sdao = new StuDao();//实例化 验证方法
	FlowPane fp = new FlowPane();//流式布局 对象
	GridPane gp = new GridPane();//网格布局 对象
	Scene scene = new Scene(fp, 300, 300);//布置 舞台
	
	//控件       	3*2 三行两列
	//用户名
	Label l1 = new Label("用户名");
	TextField user = new TextField();
	//用户密码
	Label l2 = new Label("用户密码");
	PasswordField pwd = new PasswordField();
	//按钮
	Button b = new Button("登录");
	

	public void start(Stage stage) throws Exception {
		//控件放到布局中
		gp.getChildren().addAll(l1,user,l2,pwd,b);//先得到子节点,再添加控件
		//网格布局需 行*列 格式放置控件
		gp.add(l1, 0, 0);
		gp.add(user, 1, 0);//用户名
		gp.add(l2, 1, 0);
		gp.add(pwd, 1, 1);//用户密码
		gp.add(b, 2, 0);//登录 按钮
		
		//网格布局 整体居中
		gp.setAlignment(Pos.CENTER);//Pos->Position 位置
		gp.setHgap(15);//H:水平   水平间距 px
		gp.setVgap(15);//V:垂直   垂直间距 px
		
		//点击事件: login 登录
		b.setOnAction(a->{
			//获取输入的值
			String name = user.getText();
			String password = pwd.getText();
			//参数传到数据库内,调用方法
			Student s = sdao.login(name, password);//学生对象?!  返回 null 或 s 学生对象
			//判断登录 成功 or 失败
			if(s!=null) {//有这个学生对象,查询成功
				Alert al = new Alert(AlertType.INFORMATION, "登录成功,欢迎 "+s.getName());
				al.showAndWait();//弹窗 等待
			}else {//无此对象,查询失败
				Alert al = new Alert(AlertType.WARNING, "登录失败");
				al.showAndWait();//弹出 并显示
			}
		});
		stage.setScene(scene);//场景和 舞台绑定
		stage.show();//显示 舞台
	}
	
	public static void main(String[] args) {
		Application.launch();
	}

}

注册 register

package com.zking.test;

import java.util.Optional;
import java.util.StringJoiner;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * Regist 注册
 * @author Administrator
 *
 */
public class Regist extends Application{
	//网格布局  
	GridPane gp = new GridPane();
	Scene scene = new Scene(gp, 250, 300);
	
	//控件 6*2
	//账户
	Label l1 = new Label("用户名");
	TextField user = new TextField();//输入框
	
	//密码 
	Label l2 = new Label("密码");
	PasswordField password = new PasswordField();//密码框
	
	//单选框  RadioButton
	Label l3 = new Label("性别");
	//默认可多选,必须放在同一组才可实现 单选功能
	ToggleGroup group = new ToggleGroup();//新建 组名
	
	RadioButton nan = new RadioButton("男");
	RadioButton nv = new RadioButton("女");//性别选择
	//一个位置只能放一个子节点,多个控件放置于布局内可实现
	HBox sex = new HBox(nan,nv);
	/*
	 * 流式布局
	 * FlowPane fp = new FlowPane(nan,nv);
	 * 占用舞台位置太长,更优选择 水平布局
	 * HBox 水平布局:只有一行,所有控件都在一行
	 * VBox 垂直布局:只有一列,所有控件都在一列
	 */
	
	//下拉框 ChoiceBox
	Label l4 = new Label("地址");
	ChoiceBox<String> c = new ChoiceBox<String>();//也是一个 List集合
	
	//多选框 CheckBox 
	Label l5 = new Label("爱好");
	CheckBox c1 = new CheckBox("吃饭");
	CheckBox c2 = new CheckBox("睡觉");
	CheckBox c3 = new CheckBox("打豆豆");
	//三变一
	HBox like = new HBox(c1,c2,c3);
	
	//按钮
	Button b1 = new Button("注册");
	Button b2 = new Button("取消");
	
	public void start(Stage stage) throws Exception {
		//布局 位置
		gp.add(l1, 0, 0);
		gp.add(user, 1, 0);//账户
		
		gp.add(l2, 0, 1);
		gp.add(password, 1, 1);//密码
		
		gp.add(l3, 0, 2);
		gp.add(sex, 1, 2);//性别
		nan.setToggleGroup(group);
		nv.setToggleGroup(group);//放置同一组,实现单选功能
		nan.setSelected(true);//默认性别 男
		
		gp.add(l4, 0, 3);
		gp.add(c, 1, 3);//地址
		c.getItems().addAll("湖南","湖北","四川","广东","河南");//设置下拉选项
		c.setValue("湖南");//默认地址 湖南
		
		gp.add(l5, 0, 4);
		gp.add(like, 1, 4);//爱好
		
		gp.add(b1, 0, 5);
		gp.add(b2, 1, 5);//按钮
		
		//点击事件
		//1、注册
		b1.setOnAction(a->{
			//获取输入值
			String name = user.getText();//用户名
			String pwd = password.getText();//密码
			
			String sex = "男";//默认为男性
			if(nv.isSelected()) {
				sex = "女";//性别 女 被选中
			}//性别
			String address = c.getValue();//地址
			//字符连接器 StringJoiner
			StringJoiner j = new StringJoiner(",");//美化多选结果
			if(c1.isSelected()) {
				j.add(c1.getText());
			}//爱好:吃饭
			if(c2.isSelected()) {
				j.add(c2.getText());
			}//爱好:睡觉
			if(c3.isSelected()) {
				j.add(c3.getText());
			}//爱好:打豆豆
			String hobby = j.toString();//爱好 全部存进来
			//TODO:将数据放到数据库,插入
			//??? 作业  ???
			
			//打印
			System.out.println(name);
			System.out.println(pwd);
			System.out.println(sex);
			System.out.println(address);
			System.out.println(hobby);
		});
		//2、取消
		b2.setOnAction(a->{//要理解 要理解 要理解
			Optional<ButtonType> b = new Alert(AlertType.CONFIRMATION,"你真的要退出嘛 (o°ω°o)",ButtonType.YES,ButtonType.NO).showAndWait();
			if(b.get()==ButtonType.YES) {
				Platform.exit();//关闭界面 (和 X效果相同)
			}
		});
		
		//设置网格布局
		gp.setAlignment(Pos.CENTER);
		gp.setHgap(15);//水平间距
		gp.setVgap(15);//垂直间距
		
		stage.setScene(scene);//场景 和舞台 绑定
		stage.show();//显示 舞台
	}
	
	public static void main(String[] args) {
		Application.launch();
	}
}