JavaFx学习记录

一个基本窗口

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;



public class Main extends Application {

	Button button2;

	@Override
	//start()方法是所有JavaFX应用程序的入口。
	public void start(Stage primaryStage) throws Exception{
		primaryStage.setTitle("Title of the Window");//修改窗口标题

		button2 =new Button(); //新建按钮
		button2.setText("Click me too");//修改按钮显示文本
		//向按钮添加事件 
		button2.setOnAction(new EventHandler<ActionEvent>(){
			public void handle(ActionEvent e){
				System.out.println("you clicked the button");
			}
		});
		StackPane layout = new StackPane();  //新建布局
		layout.getChildren().add(button2);//将按钮添加到布局
		Scene scene = new Scene(layout,300,250);//设置场景大小
		primaryStage.setScene(scene);//将场景放入舞台
		primaryStage.show();//显示舞台
	}

	public static void main(String[] args) {
		launch(args);
	}

}

效果:在这里插入图片描述

窗口跳转

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main2 extends Application {

	Stage window;//创建一个舞台
	Scene scene1,scene2;//创建两个场景
	@Override
	public void start(Stage primaryStage) throws Exception{
		window = primaryStage;

		Label label1=new Label("Welcome to the first scene!");//创建一个标签
		Button button1=new Button("Go to scene 2");//创建一个按钮
		button1.setOnAction(e ->window.setScene(scene2));//添加按钮事件————将场景二放入舞台

		VBox layout1 = new VBox(20); //创建一个Vbox布局
		layout1.getChildren().addAll(label1,button1);
		scene1 = new Scene(layout1,200,200);

		Button button2 = new Button("This scene sucks, go back to scene 1");
		button2.setOnAction(e ->window.setScene(scene1));

		StackPane layout2 =new StackPane();  //创建一个StackPane布局
		layout2.getChildren().add(button2);
		scene2 =new Scene(layout2,600,300);

		window.setScene(scene1); //将场景一放入舞台
		window.setTitle("Title here");
		window.show();//显示舞台
	}

	public static void main(String[] args) {
		launch(args);
	}
}


button1.setOnAction(e ->window.setScene(scene2));中的e ->window.setScene(scene2)是JavaFx的新语法,支持在JDK1.8及以上版本使用。
效果:在这里插入图片描述在这里插入图片描述

提示窗

package application;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.geometry.*;
public class AlertBox {
	public static void display(String title,String message){//通过添加形参获取提示窗标题及内容
		Stage window = new Stage();

		window.initModality(Modality.APPLICATION_MODAL);//功能是窗口的阻塞模式,简单来说 当提示窗弹出,阻止用户操作其他窗口。
		window.setTitle(title);
		window.setMinWidth(250);

		Label label = new Label();
		label.setText(message);
		Button closeButton = new Button("Close the window");
		closeButton.setOnAction(e ->window.close());

		VBox layout =new VBox(10);
		layout.getChildren().addAll(label,closeButton);
		layout.setAlignment(Pos.CENTER);

		Scene scene = new Scene(layout);
		window.setScene(scene);
		window.showAndWait();
	}
}

测试


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main3 extends Application {

	Stage window;
	Button button;
	@Override
	public void start(Stage primaryStage) {
		window = primaryStage;
		window.setTitle("thenewboston");
		button = new Button("Click me");
		button.setOnAction(new EventHandler<ActionEvent>(){
			public void handle(ActionEvent e){
				AlertBox.display("Title of Window", "Wow this alert box is awesome!");
			}});
		StackPane layout = new StackPane();
		layout.getChildren().add(button);
		Scene scene = new Scene(layout,300,250);
		window.setScene(scene);
		window.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}

效果在这里插入图片描述 在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值