在JDK13中使用javafx11

在JDK13中使用javafx11

环境描述

  • IDE:eclipse
  • JDK:jdk13

问题描述

  • Java课程老师布置了Java课程设计,其中要用到JavaFX,我在自己的电脑中敲代码时,发现没有javafx库,于是就上网查了查,得到的结果是:Java中的图形界面设计从JDK11开始,Oracle将JavaFX从JDK中删除。那我就去OpenFX下载了javafx11,将包通过buildpath的方式加入到了我的工程中(注意:要把scr.zip源码也导入,这样的话start()中的参数就不是arg0,是primaryStage了),但运行时说缺少 JavaFX 运行时组件,那么要怎么办呢?

解决方案1

  • 修改VM的运行时的参数
  • 具体操作为:
    在eclipse中执行 Run->Run Configuration->Arguments->VM Arguments,在VM Arguments中添加参数--module-path "F:\JAVA\JavaFx\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml路径换成你自己的

解决方案2

  • 将main函数单独放在一个类中。
  • 通过Application.launch(App.class, args)的方式启动窗口
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,以下是一个简单的贪吃蛇小游戏的 JavaFX 代码示例: Main.java: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ BorderPane root = new BorderPane(); Scene scene = new Scene(root, 400, 400); primaryStage.setTitle("贪吃蛇小游戏"); primaryStage.setScene(scene); primaryStage.show(); Game game = new Game(root); game.start(); } public static void main(String[] args) { launch(args); } } ``` Game.java: ```java import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.util.Duration; import java.util.Random; public class Game { private static final int WIDTH = 20; private static final int HEIGHT = 20; private static final int CELL_SIZE = 20; private static final int SPEED = 200; private BorderPane root; private Rectangle[][] cells; private int[][] map; private int snakeLength; private int snakeX; private int snakeY; private int directionX; private int directionY; private Timeline timeline; public Game(BorderPane root) { this.root = root; cells = new Rectangle[WIDTH][HEIGHT]; map = new int[WIDTH][HEIGHT]; snakeLength = 3; snakeX = WIDTH / 2; snakeY = HEIGHT / 2; directionX = 1; directionY = 0; } public void start() { init(); timeline = new Timeline(new KeyFrame(Duration.millis(SPEED), event -> { update(); })); timeline.setCycleCount(Animation.INDEFINITE); timeline.play(); } private void init() { for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { Rectangle rect = new Rectangle(i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE); rect.setFill(Color.WHITE); rect.setStroke(Color.BLACK); root.getChildren().add(rect); cells[i][j] = rect; } } map[snakeX][snakeY] = snakeLength; updateCell(snakeX, snakeY, Color.GREEN); createFood(); } private void update() { int newSnakeX = snakeX + directionX; int newSnakeY = snakeY + directionY; if (newSnakeX < 0 || newSnakeX >= WIDTH || newSnakeY < 0 || newSnakeY >= HEIGHT) { gameOver(); return; } if (map[newSnakeX][newSnakeY] > 0 && map[newSnakeX][newSnakeY] < snakeLength) { gameOver(); return; } if (map[newSnakeX][newSnakeY] == -1) { snakeLength++; createFood(); } updateCell(snakeX, snakeY, Color.WHITE); updateCell(newSnakeX, newSnakeY, Color.GREEN); map[snakeX][snakeY] = snakeLength; snakeX = newSnakeX; snakeY = newSnakeY; for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { if (map[i][j] > 0) { map[i][j]--; } } } } private void gameOver() { timeline.stop(); } private void updateCell(int x, int y, Color color) { cells[x][y].setFill(color); } private void createFood() { Random random = new Random(); int x, y; do { x = random.nextInt(WIDTH); y = random.nextInt(HEIGHT); } while (map[x][y] != 0); map[x][y] = -1; updateCell(x, y, Color.RED); } public void setDirection(int directionX, int directionY) { this.directionX = directionX; this.directionY = directionY; } } ``` 在这个示例,我们使用一个二维矩阵来表示地图,其 0 表示空格,正整数表示蛇的身体长度,-1 表示食物。游戏通过 Timeline 实现动画效果,每隔一定时间调用 update 方法来更新游戏状态。当蛇撞到墙壁或自身时,游戏结束。可以通过 setDirection 方法来改变蛇的移动方向。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值