Javafx之舞台,场景,控件及方法

一、基本操作

package com.zking.test;

import javafx.application.Application;
import javafx.stage.Stage;

public class One extends Application{//1/继承Application
	//2、重写start
	public void start(Stage stage) throws Exception {
		stage.setTitle("我的第一个Javafx");//设置标题
		stage.show();//显示舞台
		//舞台大小  单位:px 像素点
		stage.setWidth(400);//设置宽度
		stage.setHeight(300);//设置高度
	}
	
	//3、main方法调用lauch()
	public static void main(String[] args) {
		Application.launch();//Application是Javafx入口
	}
}

二、控件,固定舞台

package com.zking.test;

import javafx.application.Application;
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.FlowPane;
import javafx.stage.Stage;

public class Two extends Application{//1、继承Application类
	//2、重写 start方法
	public void start(Stage s) throws Exception {
		//流式布局 FlowPane
		//按顺序依次排放,填满自动换下一行
		FlowPane f = new FlowPane();
		//布局里面放控件
		/*
		 * 控件
		 * Button 按钮
		 * Label 文本标签
		 * TextField 文本框
		 * PasswordField 密码框:自动加密
		 */
		Button b1 = new Button("我是按钮1");
		Button b2 = new Button("我是按钮2");
		Label l1 = new Label("宇宙大爆炸");
		Label l2 = new Label("太阳的起源");
		TextField t = new TextField("这是文本框");
		PasswordField p = new PasswordField();
		
		//设置文本内容
		b2.setText("点击 按钮1");
		//获取文本内容
		b1.getText();
		
		//给b2设置点击事件
		b2.setOnAction(a->{//lamb方法,简化
			System.out.println("perfe +1");
		});
		
	
		/*
		 * f.getChildren() 是一个 List集合
		 * 调用 add()方法添加一个控件
		 * 调用 addAll()方法添加全部控件
		 */
		//把控件放到布局中
		f.getChildren().addAll(b1,b2,l1,l2,t,p);
		//or
//		f.getChildren().add(b1);
//		f.getChildren().add(b2);
		
		//布局和场景绑定
		Scene scene = new Scene(f,400,300);
		//场景和舞台绑定
		s.setScene(scene);
		
		//显示舞台
		s.show();
		
		//固定 舞台大小
		s.setResizable(false);
	}
	//3、写 main方法,调用 launch方法
	public static void main(String[] args) {
		Application.launch();
	}
}

三、点击事件

package com.zking.test;

import javafx.application.Application;
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.stage.Stage;

public class Three extends Application{//继承Appliction
	//重写start
	public void start(Stage s) throws Exception {
		//流式布局
		FlowPane f = new FlowPane();
		
		//账号
		Label zh = new Label("用户名");
		TextField srk = new TextField("请输入用户");
		
		//密码
		Label mm = new Label("密码");
		PasswordField p = new PasswordField();
		
		//按钮
		Button b1 = new Button("登录");
		Button b2 = new Button("注册");
		Button b3 = new Button("忘记密码");
		//把控件放进布局中
		f.getChildren().addAll(zh,srk,mm,p,b1,b2,b3);
		
		//点击事件
		b1.setOnAction(a->{//都是登录失败
//			String sql = "select * from animal where name=? and password=?";
			String username = srk.getText();//获取用户名
			String userpassword = p.getText();//获取密码
			//前往数据库作登录验证
			if("sa".equals(username) && "123".equals(userpassword)) {
				Alert a1 = new Alert(AlertType.INFORMATION, "登录成功");
				a1.showAndWait();//显示弹窗
			}else {
				Alert a2 = new Alert(AlertType.WARNING, "登录失败");
				a2.showAndWait();//显示弹窗
			}
		});
		
		//绑定
		Scene scene = new Scene(f,200,250);//场景和布局,长宽
		s.setScene(scene);//场景和舞台
		
		//显示舞台
		s.setTitle("1.0 Javafx");
		s.show();
	}
	
	//main方法
	public static void main(String[] args) {
		Application.launch();
	}
}

四、边框布局

package com.zking.test;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Four extends Application{
	//重写start方法
	public void start(Stage stage) throws Exception {
		//边框布局
		BorderPane borderPane = new BorderPane();
		//新建控件
		Button b1 = new Button("中间");
		Button b2 = new Button("下面");
		Button b3 = new Button("上面");
		Button b4 = new Button("左边");
		Button b5 = new Button("右边");
		//把控件放到布局对应位置
		borderPane.setCenter(b1);
		borderPane.setBottom(b2);
		borderPane.setTop(b3);
		borderPane.setLeft(b4);
		borderPane.setRight(b5);
		//为布局生成场景
		Scene scene = new Scene(borderPane, 400, 400);
		//将场景绑定到舞台
		stage.setScene(scene);
		//显示舞台
		stage.show();
		
	}
	
	//main方法调用 launch()方法
	public static void main(String[] args) {
		Application.launch();
	}
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youdabi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值