JavaFX Hello World示例

JavaFX是一组图形和媒体包,使开发人员能够设计,创建,测试,调试和部署可在各种平台上一致运行的富客户端应用程序。
(来源: Oracle

从JDK 7u6开始,如果您使用的是较早的Java安装,则标准JDK和JRE捆绑包中包含JavaFX,请在此处更新或导航。

在Eclipse IDE + Java 8中访问JavaFX
默认情况下,当我们访问JavaFX API时,Eclipse将提示Access restriction错误。 要解决此问题, 请安装e(fx)clipse插件

Java 8中的JavaFX
在Java 8中,JavaFX不是JavaSE执行环境的一部分,而是作为一个外部库– lib\ext\jfxrt.jar

JavaFX Hello世界示例

一个简单的示例,其中我们使用GridPane布置绘制背景的4个Rectangles以及ButtonLabelBorderPane

JavaFxHelloWorld.java
package com.mkyong.helloworld;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class JavaFxHelloWorld extends Application {
	
	private int counter = 0;

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

	@SuppressWarnings("static-access")
	public void start(Stage stage) {
		stage.setTitle("Hello World!");

		GridPane grid = new GridPane();
		grid.setHgap(0);
		grid.setVgap(0);

		Rectangle r1 = new Rectangle();
		r1.setFill(Color.DARKKHAKI);
		r1.setHeight(125);
		r1.setWidth(125);

		Rectangle r2 = new Rectangle();
		r2.setFill(Color.rgb(189, 40, 40));
		r2.setHeight(125);
		r2.setWidth(125);

		Rectangle r3 = new Rectangle();
		r3.setFill(Color.hsb(235, 0.52, 0.36));
		r3.setHeight(125);
		r3.setWidth(125);

		Rectangle r4 = new Rectangle();
		r4.setFill(Color.web("b894cc"));
		r4.setHeight(125);
		r4.setWidth(125);

		grid.add(r1, 0, 0);
		grid.add(r2, 0, 1);
		grid.add(r3, 1, 0);
		grid.add(r4, 1, 1);

		Label l = new Label();
		l.setFont(new Font("Calibri", 15));
		l.setTextFill(Color.BLACK);

		Button button = new Button();
		button.setFont(new Font("Calibri", 15));
		button.setText("Say 'Hello World'");

		button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				l.setText("You said Hello to the world: 
					" + ++counter + ((counter == 1) ? " time" : " times"));
			}
		});

		BorderPane bp = new BorderPane();
		bp.setBottom(l);
		bp.setAlignment(l, Pos.CENTER);
		bp.setCenter(button);

		StackPane root = new StackPane();
		root.getChildren().add(grid);
		root.getChildren().add(bp);
		stage.setScene(new Scene(root, 250, 250));
		stage.show();
		
	}
	
}

输出:

javafx-helloworld-1

一次单击“说'Hello World'”按钮时:

javafx-helloworld-2

多次单击“说'Hello World'”按钮时:

javafx-helloworld-3

参考文献

  1. Oracle JavaFX场景图
  2. 在JavaFX中使用布局
  3. 维基百科JavaFX
  4. 在JRE 8中使用JavaFX
  5. e(fx)剪辑插件

翻译自: https://mkyong.com/javafx/javafx-hello-world-example/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值