首先,javafx有点像前端,我们可以把他理解为这样的结构
分别为
窗口:就是显示的窗口
场景:相当于页面,整个可操作的大小,估计可以切换不同场景
节点:就是内部各个模块,自己一个个设置
代码如下
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
/*Application类还能获取主机网址
* */
}
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("你好javaFX");
/*标签放到布局里*/
BorderPane pane = new BorderPane(label);
/*布局放到场景里,设置高度宽度*/
Scene scene = new Scene(pane,300,300);
/*场景放到窗口里*/
stage.setScene(scene);
stage.setTitle("窗口");
stage.show();
/*
* Stage 窗口 下面是
* Scene 场景 下面是
* parent 根节点 下面是
* 节点 note label
* */
System.out.println("调用主程序...");
}
@Override
public void init() throws Exception{
super.init();
System.out.println("开始...");
/*进行初始化工作,比如建立数据库链接
* 新建线程与窗口同步进行
* */
}
@Override
public void stop() throws Exception{
System.out.println("结束...");
/*进行清理数据库链接工作,
* 清理资源链接
*
* */
}
}
显示如下,我们需要注意的是其实就是一个套一个,不同节点(控件)传入场景,不同场景传入窗口
而调用时会调用另外两个父类方法,我们给他重写,发现一个是启动方法,一个是结束方法
一个运行在主程序开始之前,一个运行在主程序结束
用途就可以多样化比如
启动方法:进行初始化工作,比如建立数据库链接
新建线程与窗口同步进行
结束方法:进行清理数据库链接工作,
清理资源链接
Application类还能获取主机网址
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
/*Application类还能获取主机网址
* */
}
@Override
public void start(Stage stage) throws Exception {
Button button = new Button("按钮");
BorderPane pane = new BorderPane(button);
button.setOnAction(e ->{
getHostServices().showDocument("www.bilibili.com");
});
/*布局放到场景里,设置高度宽度*/
Scene scene = new Scene(pane,300,300);
/*场景放到窗口里*/
stage.setScene(scene);
stage.setTitle("窗口");
stage.show();
System.out.println("调用主程序...");
}
}
运行结果是
我们可以知道显示什么内容(比如这里的按钮)只需要传入不同对象
Label label = new Label("你好javaFX");
BorderPane pane = new BorderPane(label);
Button button = new Button("按钮");
BorderPane pane = new BorderPane(button);
我们还利用了这个类的调用网址的功能
调用了小破站
button.setOnAction(e ->{
getHostServices().showDocument("www.bilibili.com");
})
Stage类
Titel:设置窗口名称
icon:设置窗口图标
resiziable:设置窗口是否能调节大小
x,y,width,height:设置原始宽高
StageStyle:设置窗口样式,4种
在添加图标时
放入图片
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle(" Hello");
primaryStage.getIcons().add(new Image("image/icon.ico"));
primaryStage.show();
}
}
视频中可以不过不知道为什么报错
Exception in Application start method
网上找方法后发现这样可以
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.File;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
String path = "image/img.png";
File fileIcon = new File(path);
Image applicationIcon = new Image(fileIcon.toURI().toString());
primaryStage.getIcons().add(applicationIcon);
primaryStage.setTitle(" Hello");
Label label = new Label("添加图标");
BorderPane pane = new BorderPane(label);
Scene scene = new Scene(pane,300,200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
String path = "image/img.png";
File fileIcon = new File(path);
Image applicationIcon = new Image(fileIcon.toURI().toString());
primaryStage.getIcons().add(applicationIcon);
看上去像是没法直接通过路径识别图片文件,进行了转换
primaryStage.getIcons().add(new Image("image/icon.ico"));
那我们可以改成
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.File;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));
primaryStage.setTitle(" Hello");
Label label = new Label("添加图标");
BorderPane pane = new BorderPane(label);
Scene scene = new Scene(pane,300,200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
效果如下
4种窗口样式
StageStyle.DECORATED
默认,和不设置一样
StageStyle.UNDECORATED
无装饰,上面那一栏没有
StageStyle.TRANSPARENT
透明窗口
StageStyle.UTILITY
简单装饰
没啥太大区别,一般使用默认
下面是点击跳转另一个页面
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));
primaryStage.setTitle(" Hello");
Button button1 = new Button("按钮1");
Button button2 = new Button("按钮2");
button1.setLayoutX(200);
button1.setLayoutY(200);
button2.setLayoutX(200);
button2.setLayoutY(250);
button1.setOnAction(e ->{
getHostServices().showDocument("www.bilibili.com");
});
button2.setOnAction(event ->{
Stage stage = new Stage();
stage.setHeight(200);
stage.setWidth(600);
stage.initModality(Modality.NONE);
stage.show();
});
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button2,button1);
Scene scene = new Scene(pane,500,500);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.show();
}
}
效果如下
无修
Modality.NONE
这时候我们发现,Stage不就是窗口吗,那我们可以在新的窗口中继续操作,于是我们就可以
无限套娃
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));
primaryStage.setTitle(" Hello");
Button button1 = new Button("按钮1");
Button button2 = new Button("按钮2");
button1.setLayoutX(200);
button1.setLayoutY(200);
button2.setLayoutX(200);
button2.setLayoutY(250);
button1.setOnAction(e ->{
getHostServices().showDocument("www.bilibili.com");
});
button2.setOnAction(event ->{
Button button3 = new Button("按钮1");
Button button4 = new Button("按钮2");
button3.setLayoutX(200);
button3.setLayoutY(200);
button4.setLayoutX(200);
button4.setLayoutY(250);
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button3,button4);
Scene scene = new Scene(pane,500,500);
Stage stage = new Stage();
stage.setHeight(400);
stage.setWidth(400);
stage.initModality(Modality.NONE);//修饰格式,Modality.NONE表示无修饰
stage.setScene(scene);
stage.setTitle("窗口");
stage.show();
});
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button2,button1);
Scene scene = new Scene(pane,500,500);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.show();
}
}
这里发现button名字不能重复,我们要换成不同的名字
设置
stage.initOwner(primaryStage);
再设置
WINDOW_MODAL
设置这个时,副窗口激活,主窗口不能用,其他都可以
APPLICATION_MODAL
设置这个时,副窗口激活,其他窗口不能用
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));
primaryStage.setTitle(" Hello");
Button button1 = new Button("按钮1");
Button button2 = new Button("按钮2");
button1.setLayoutX(200);
button1.setLayoutY(200);
button2.setLayoutX(200);
button2.setLayoutY(250);
button2.setOnAction(event ->{
Button button3 = new Button("按钮1");
Button button4 = new Button("按钮2");
button3.setLayoutX(200);
button3.setLayoutY(200);
button4.setLayoutX(200);
button4.setLayoutY(250);
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button3,button4);
Scene scene = new Scene(pane,500,500);
Stage stage = new Stage();
stage.setHeight(400);
stage.setWidth(400);
stage.initModality(Modality.APPLICATION_MODAL);//修饰格式,Modality.NONE表示无修饰
//Modality.APPLICATION_MODAL 全局应用模态,其他窗口借用
//Modality.WINDOW_MODAL 要有副窗口
stage.setScene(scene);
stage.initOwner(primaryStage);
stage.setTitle("窗口");
stage.show();
});
button1.setOnAction(event ->{
Stage stage = new Stage();
stage.setHeight(400);
stage.setWidth(400);
stage.setTitle("窗口");
stage.show();
});
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button2,button1);
Scene scene = new Scene(pane,500,500);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.show();
}
}
关闭窗口事件,其他大同小异
package cc.caiguang.hello;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;
import java.util.Optional;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));
primaryStage.setTitle(" Hello");
Button button1 = new Button("按钮1");
Button button2 = new Button("按钮2");
button1.setLayoutX(200);
button1.setLayoutY(200);
button2.setLayoutX(200);
button2.setLayoutY(250);
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button2,button1);
Scene scene = new Scene(pane,500,500);
Platform.setImplicitExit(false);
//取消操作系统默认退出动作
primaryStage.setOnCloseRequest(e ->{
e.consume();
//取消关闭窗口动作
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("退出程序");
alert.setHeaderText(null);
alert.setContentText("是否退出");
//弹出框,是否退出
Optional<ButtonType> result = alert.showAndWait();
if (result.get() ==ButtonType.OK){
Platform.exit();
//系统关闭应用
}
});
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.show();
}
}
展示效果
这个是关闭窗口,结束运行程序
还有
Platform.exit();
//系统关闭应用
primaryStage.close();
//仅关闭窗口,不结束程序
以上就是今天学习内容,有人知道那个图片的为啥直接写报错吗