`
weiwei5910
  • 浏览: 26843 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类

javaFx2.x 登陆及其切换界面

 
阅读更多

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

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


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

效果如下图





下面是代码


程序的入口

LoginWidthCss.class

package com.wei.table;

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

import com.wei.jdbc.JdbcUtil;

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.text.Text;
import javafx.stage.Stage;

/**
 * 登陆首页面
 * @author wangwei
 *
 */
public class LoginWidthCss extends Application {

	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	
	private GridPane grid;
	public static void main(String[] args) {
	   launch(args );
	}
	
	@Override
	public void start(final Stage primaryStage) throws Exception {
		//窗口标题
		primaryStage.setTitle("JavaFX 登陆");
		//设置面板及布局
		grid = new GridPane();
		grid.setAlignment(Pos.CENTER);
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(25));
		//文本框
		Text scenetitle = new Text("Welcome");
		scenetitle.setId("welcome-text");
		grid.add(scenetitle, 0, 0, 2, 1);
		//标签
		Label userName = new Label("用户名:");
		grid.add(userName, 0, 1);
		//文本输入框
		final TextField userTextField = new TextField();
		grid.add(userTextField, 1, 1);
		
		Label passwd = new Label("密码");
		grid.add(passwd, 0, 2);
		//密码输入框
		final PasswordField passwdField = new PasswordField();
		grid.add(passwdField, 1, 2);
		//按钮及按钮布局
		Button btn = new Button("登陆");
		HBox hbBtn = new HBox(10);
		hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
		hbBtn.getChildren().add(btn);
		grid.add(hbBtn, 1, 4);
		
		//点击登陆按钮显示的提示文字
		final Text actiontarget = new Text();
		actiontarget.setId("actiontarget");
		grid.add(actiontarget, 1, 6);

		//登陆按钮单击事件
		btn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				actiontarget.setText("登陆成功");
				grid.setVisible(false);
				//表格面板
				TablePane tp = new TablePane();
				//加到场景中
				Scene tpScene = new Scene(tp,500,500);
				//切换舞台场景为表格面板
				primaryStage.setScene(tpScene);
				
			/*	//获取用户名,密码的值
				String userName = userTextField.getText();
				String passWord = passwdField.getText();
				//建立连接查询数据库
				conn = JdbcUtil.getConn();
				String sql = "select t.*, t.rowid from itcsys_user t where t.usercode='"+userName+"' and t.username='"+passWord+"'";
				try {
					ps = conn.prepareStatement(sql);
					rs = ps.executeQuery();
					if(rs.next()){
						actiontarget.setText("登陆成功");
						grid.setVisible(false);
						//表格面板
						TablePane tp = new TablePane();
						//加到场景中
						Scene tpScene = new Scene(tp,500,500);
						//切换舞台场景为表格面板
						primaryStage.setScene(tpScene);
					}else{
						actiontarget.setText("登陆失败");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally{
					JdbcUtil.close(rs,ps,conn);
				}*/
			}
		});
		Scene scene = new Scene(grid,300,275);
		//为场景加入CSS样式
		scene.getStylesheets().add(LoginWidthCss.class.getResource("LoginWidthCss.css").toExternalForm());
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
}


TablePane.class

package com.wei.table;

import com.wei.table.TableSample.scoreInfo;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;

/**
 * 列表面板
 * @author wangwei
 *
 */
public class TablePane extends AnchorPane {
	
	public TablePane() {
		//列表的列
		TableColumn lshColumn = new TableColumn();
		//列名
		lshColumn.setText("流水号");
		//列宽
		lshColumn.setMinWidth(100);
		//该列绑定的数据
		lshColumn.setCellValueFactory(new PropertyValueFactory("lsh"));
		
		TableColumn ksrqColumn = new TableColumn();
		ksrqColumn.setText("考试日期");
		ksrqColumn.setMinWidth(100);
		ksrqColumn.setCellValueFactory(new PropertyValueFactory("ksrq"));
		
		TableColumn kscjColumn = new TableColumn();
		kscjColumn.setText("考试成绩");
		kscjColumn.setMinWidth(100);
		kscjColumn.setCellValueFactory(new PropertyValueFactory("score"));
		
		//创建一个列表
		TableView tableView = new TableView();
		//在列表中加入列
		tableView.getColumns().addAll(lshColumn,ksrqColumn,kscjColumn);
		//表格的数据源
		ObservableList<scoreInfo> data = FXCollections.observableArrayList();
		//列表赋值
		data.add(new scoreInfo("1", "2", "3"));
		//为列表绑定数据
		tableView.setItems(data);
		//把列表加到面板中
		this.getChildren().add(tableView);
	}
}


切换窗口本来想做成点击登陆按钮后,隐藏当前窗口再显示新的窗口,找了很久也没能找到方法

最终使用切换面板这么个笨方法来实现,如果大家有更好的方法,请指点


文笔不太好,附上源码

http://download.csdn.net/detail/weiwei5910/5430339




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics