编程语言
首页 > 编程语言> > JavaFX基础:5:使用ActionEvent响应

JavaFX基础:5:使用ActionEvent响应

作者:互联网

前面几篇文章介绍了JavaFX的基础知识,也提到了JavaFX可以进行客户端桌面应用的开发,这篇文章给上述简单的功能添加2个按钮,可以根据按钮的进行新的window的显示和隐藏。

代码结构

liumiaocn:JavaFXButton liumiao$ ls
pom.xml src
liumiaocn:JavaFXButton liumiao$ tree .
.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── liumiaocn
        │           └── javademo
        │               └── Main.java
        └── resources

7 directories, 2 files
liumiaocn:JavaFXButton liumiao$ 

为了简单,这里仅剩下2个文件,一个pom.xml文件,一个Main.java文件

pom.xml

pom.xml文件内容如下所示,具体介绍可参看:

liumiaocn:JavaFXButton liumiao$ cat pom.xml 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.liumiaocn</groupId>
    <artifactId>javademo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>javafxdemo</name>
    <description>JavaFX demo project</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.liumiaocn.javademo.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
liumiaocn:JavaFXButton liumiao$

Main.java

liumiaocn:JavaFXButton liumiao$ cat src/main/java/com/liumiaocn/javademo/Main.java 
package com.liumiaocn.javademo;

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.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		final Stage window = new Stage();
		window.setX(40);
		Scene innerScene = new Scene(new Label("Show or Hide Me"),200,100);
		window.setScene(innerScene);
                window.setTitle("Show/Hide Window");

		HBox root = new HBox(200d);
		Button showButton = new Button("Show Window");
		showButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				window.show();
			}
		});
		Button hideButton = new Button("Hide Window");
		hideButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				window.hide();
			}
		});
		root.getChildren().add(showButton);
		root.getChildren().add(hideButton);
                primaryStage.setTitle("Hello LiuMiao");
		primaryStage.setScene(new Scene(root,600,600));
                
		primaryStage.show();
	}
}
liumiaocn:JavaFXButton liumiao$

执行构建并生成jar文件

liumiaocn:JavaFXButton liumiao$ pwd
/Users/liumiao/easypack/containers/alpine/maven/demo/JavaFXButton
liumiaocn:JavaFXButton liumiao$ mvn clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.liumiaocn:javademo >-----------------------
[INFO] Building javafxdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ javademo ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javademo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/liumiao/easypack/containers/alpine/maven/demo/JavaFXButton/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javademo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/liumiao/easypack/containers/alpine/maven/demo/JavaFXButton/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ javademo ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javademo ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ javademo ---
[INFO] Building jar: /Users/liumiao/easypack/containers/alpine/maven/demo/JavaFXButton/target/javademo-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.474 s
[INFO] Finished at: 2019-05-03T21:28:28+08:00
[INFO] ------------------------------------------------------------------------
liumiaocn:JavaFXButton liumiao$ 

结果确认

使用java -jar即可运行程序

liumiaocn:JavaFXButton liumiao$ java -jar target/javademo-0.0.1-SNAPSHOT.jar 

在这里插入图片描述
点击Show Window即可显示新的窗口,点击Hide Window即可隐藏此窗口
在这里插入图片描述

当然也可以使用maven容器方式进行构建,详细可参看如下文章

标签:INFO,javademo,JavaFX,maven,响应,liumiao,JavaFXButton,ActionEvent,liumiaocn
来源: https://blog.csdn.net/liumiaocn/article/details/89791679