当前位置:   article > 正文

基于javafx,通过fxml实现交互的简单石头剪刀布游戏

基于javafx,通过fxml实现交互的简单石头剪刀布游戏

一:前言

        在经历了一段时间的新学习(折磨加摸鱼)后,本人在上一篇博文的基础上制作了这一次的作品,说实话,fxml的使用就我目前感觉来,其实更加偏向实际操作,理论知识可能用到的较少(也有可能是我太菜了),废话不多说,进入本次的主题吧!由于本人技术有限,期待各位读者在阅读后给出指正,谢谢!

二:总览


        


        主要还是有三个部分,登录注册,游戏画面,以及其他操作具体的简介已经放在代码注释中了,诸位读者请慢慢食用!

        另外,也先奉上fxml页面:





//由于本人写下这份博客匆忙,所以当前游戏时间暂时没有及时跟上

三:具体代码

 1.gameView包

(1)GameAction类

  1. package myRPSGame.gameView;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.fxml.FXML;
  5. import javafx.fxml.Initializable;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.ListView;
  8. import javafx.scene.control.TextField;
  9. import myRPSGame.MainApp;
  10. import myRPSGame.otherConduct2.DisPlayDialog;
  11. import myRPSGame.otherConduct2.DisplayAlert;
  12. import myRPSGame.otherConduct2.GetCurrentTime;
  13. import myRPSGame.registerAndLogin.register.AccountList;
  14. import java.io.IOException;
  15. import java.net.URL;
  16. import java.util.ResourceBundle;
  17. public class GameAction implements Initializable {
  18. @FXML
  19. private Label username;
  20. @FXML
  21. private Label winningRate;
  22. @FXML
  23. private ListView<String> listView;
  24. @FXML
  25. private Label currentTime;
  26. @FXML
  27. private Label user;
  28. @FXML
  29. private Label computer;
  30. private UserPlay userPlay;
  31. private ObservableList<String> observableList= FXCollections.observableArrayList();
  32. private static int theAccount;
  33. @FXML
  34. private RPSView View1;
  35. @FXML
  36. private RPSView View2;
  37. public static void setTheAccount(int theAccount) {
  38. GameAction.theAccount=theAccount;
  39. }
  40. //查询游戏记录
  41. @FXML
  42. public void inquireRecord()
  43. {
  44. observableList.clear();
  45. for(int i=0;i<AccountList.getAccounts(theAccount).getRecord().getCurrentRecord();i++){
  46. observableList.add(AccountList.getAccounts(theAccount).getRecord().getTheRecord(i));
  47. }
  48. listView.setItems(observableList);
  49. }
  50. //退出
  51. @FXML
  52. public void exit() throws IOException {
  53. MainApp.setRoot("registerAndLogin/log/LoginView.fxml");
  54. }
  55. //删除游戏记录
  56. @FXML
  57. public void deleteRecord()
  58. {
  59. AccountList.getAccounts(theAccount).getRecord().deleteRecord();
  60. inquireRecord();
  61. }
  62. //开始游戏
  63. @FXML
  64. public void start()
  65. {
  66. View1.loadRandomImage(100,145);
  67. View2.loadRandomImage(100,145);
  68. AccountList.getAccounts(theAccount).setPlayNumber(AccountList.getAccounts(theAccount).getPlayNumber()+1);
  69. userPlay=new UserPlay(DisPlayDialog.inputChoice());
  70. View1.stopPicture();
  71. View2.stopPicture();
  72. userPlay.computerChoice();
  73. user.setText(userPlay.getUser());
  74. loadTheView(userPlay.getUser());
  75. computer.setText(userPlay.getComputer());
  76. loadTheView2(userPlay.getComputer());
  77. switch (userPlay.getResult())
  78. {
  79. case 0->DisplayAlert.display("游戏结束!","平局","不是哥们,这都能打平??!");
  80. case -1->{DisplayAlert.display("游戏结束","你赢了","赢个狗吧人机看把你能的。。。"); AccountList.getAccounts(theAccount).setWinningNUmber(AccountList.getAccounts(theAccount).getWinningNUmber()+1);}
  81. default -> DisplayAlert.display("游戏结束","你输了","人机都能输?哥们回去玩泥巴吧!");
  82. }
  83. AccountList.getAccounts(theAccount).setWinningRate();
  84. winningRate.setText(STR."\{AccountList.getAccounts(theAccount).getWinningRate()}%");
  85. AccountList.getAccounts(theAccount).getRecord().addRecord(STR."进行游戏 时间:\{GetCurrentTime.getCurrentTime()}");
  86. }
  87. //加载玩家和电脑选择的图片
  88. public void loadTheView(String choice)
  89. {
  90. System.out.println(choice);
  91. switch (choice)
  92. {
  93. case "石头"-> View1.loadStoneImage(100,145);
  94. case "剪刀"-> View1.loadScissorImage(100,145);
  95. default -> View1.loadClothImage(100,145);
  96. }
  97. }
  98. public void loadTheView2(String choice)
  99. {
  100. System.out.println(choice);
  101. switch (choice)
  102. {
  103. case "石头"->View2.loadStoneImage(100,145);
  104. case "剪刀"-> View2.loadScissorImage(100,145);
  105. default->View2.loadClothImage(100,145);
  106. }
  107. }
  108. //修改用户名
  109. @FXML
  110. public void changeUsername() {
  111. username.setText(DisPlayDialog.inputUsername());
  112. }
  113. //重置胜率
  114. @FXML
  115. public void overWinningRate()
  116. {
  117. winningRate.setText("100%");
  118. AccountList.getAccounts(theAccount).setWinningRate(100);
  119. AccountList.getAccounts(theAccount).setPlayNumber(0);
  120. AccountList.getAccounts(theAccount).setPlayNumber(0);
  121. }
  122. //初始化
  123. @Override
  124. public void initialize(URL location, ResourceBundle resources)
  125. {
  126. username.setText(AccountList.getAccounts(theAccount).getUserName());
  127. winningRate.setText(STR."\{AccountList.getAccounts(theAccount).getWinningRate()}%");
  128. }
  129. }

(2)RPSView类

  1. package myRPSGame.gameView;
  2. import javafx.animation.KeyFrame;
  3. import javafx.animation.Timeline;
  4. import javafx.application.Platform;
  5. import javafx.scene.image.Image;
  6. import javafx.scene.image.ImageView;
  7. import javafx.scene.layout.AnchorPane;
  8. import javafx.util.Duration;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class RPSView extends AnchorPane
  12. {
  13. private Image image;
  14. private ImageView imageView;
  15. private Timeline timeline;
  16. //加载图片并设置格式
  17. public void loadStoneImage(double height,double width)
  18. {
  19. image=new Image("/myRPSGame/gameView/resouce/微信图片_20240513135843.jpg");
  20. imageView=new ImageView();
  21. imageView.setImage(image);
  22. imageView.setFitHeight(height);
  23. imageView.setFitWidth(width);
  24. getChildren().add(imageView);
  25. }
  26. public void loadScissorImage(double height,double width)
  27. {
  28. image=new Image("/myRPSGame/gameView/resouce/微信图片_20240513135848.jpg");
  29. imageView=new ImageView();
  30. imageView.setImage(image);
  31. imageView.setFitHeight(height);
  32. imageView.setFitWidth(width);
  33. getChildren().add(imageView);
  34. }
  35. public void loadClothImage(double height,double width)
  36. {
  37. image=new Image("/myRPSGame/gameView/resouce/微信图片_20240513135820.jpg");
  38. imageView=new ImageView();
  39. imageView.setImage(image);
  40. imageView.setFitHeight(height);
  41. imageView.setFitWidth(width);
  42. System.out.println(1);
  43. getChildren().add(imageView);
  44. }
  45. //在作出选项前循环随机播放图片
  46. public void loadRandomImage(double height,double width)
  47. {
  48. List<Image> images;
  49. final int[] currentIndex = {0};
  50. // 加载图片
  51. images = Arrays.asList(
  52. new Image("/myRPSGame/gameView/resouce/微信图片_20240513135820.jpg"),
  53. new Image("/myRPSGame/gameView/resouce/微信图片_20240513135843.jpg"),
  54. new Image("/myRPSGame/gameView/resouce/微信图片_20240513135848.jpg")
  55. );
  56. imageView=new ImageView();
  57. imageView.setFitHeight(height);
  58. imageView.setFitWidth(width);
  59. imageView.setImage(images.getFirst());
  60. timeline = new Timeline(
  61. new KeyFrame(Duration.seconds(0.2), event -> {
  62. Platform.runLater(() -> {
  63. imageView.setImage(images.get(currentIndex[0]));
  64. currentIndex[0] = (currentIndex[0] + 1) % images.size();
  65. });
  66. })
  67. );
  68. timeline.setCycleCount(Timeline.INDEFINITE); // 无限循环
  69. timeline.play();
  70. getChildren().add(imageView);
  71. }
  72. //消除展示的图片
  73. public void stopPicture() {
  74. timeline.stop();
  75. }
  76. }

(3)UserPlay.fxml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.layout.*?>
  4. <?import javafx.scene.shape.*?>
  5. <?import javafx.scene.text.*?>
  6. <?import myRPSGame.gameView.RPSView?>
  7. <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myRPSGame.gameView.GameAction">
  8. <children>
  9. <Label layoutX="14.0" layoutY="14.0" prefHeight="26.0" prefWidth="45.0" text="用户:">
  10. <font>
  11. <Font name="System Bold" size="15.0" />
  12. </font>
  13. </Label>
  14. <Label layoutX="14.0" layoutY="61.0" text="胜率:">
  15. <font>
  16. <Font name="System Bold Italic" size="16.0" />
  17. </font>
  18. </Label>
  19. <Button layoutX="152.0" layoutY="16.0" mnemonicParsing="false" onAction="#changeUsername" prefHeight="23.0" prefWidth="45.0" text="修改">
  20. <font>
  21. <Font name="System Bold" size="12.0" />
  22. </font>
  23. </Button>
  24. <Button layoutX="152.0" layoutY="61.0" mnemonicParsing="false" onAction="#overWinningRate" prefHeight="23.0" prefWidth="45.0" text="重置" />
  25. <Button layoutX="19.0" layoutY="103.0" mnemonicParsing="false" onAction="#inquireRecord" prefHeight="32.0" prefWidth="90.0" text="查询游戏记录" />
  26. <Button layoutX="165.0" layoutY="103.0" mnemonicParsing="false" onAction="#start" prefHeight="31.0" prefWidth="60.0" text="PLAY">
  27. <font>
  28. <Font name="System Bold Italic" size="15.0" />
  29. </font>
  30. </Button>
  31. <ListView fx:id="listView" layoutX="17.0" layoutY="149.0" prefHeight="200.0" prefWidth="228.0" />
  32. <Button layoutX="17.0" layoutY="365.0" mnemonicParsing="false" onAction="#deleteRecord" text="删除">
  33. <font>
  34. <Font name="System Bold" size="14.0" />
  35. </font>
  36. </Button>
  37. <Label layoutX="415.0" layoutY="8.0" text="当前时间:" />
  38. <Label fx:id="currentTime" layoutX="475.0" layoutY="8.0" prefHeight="15.0" prefWidth="117.0" text="CurrentTime" />
  39. <Button layoutX="175.0" layoutY="360.0" mnemonicParsing="false" onAction="#exit" text="退出">
  40. <font>
  41. <Font size="13.0" />
  42. </font>
  43. </Button>
  44. <Label fx:id="username" layoutX="64.0" layoutY="12.0" prefHeight="30.0" prefWidth="70.0">
  45. <font>
  46. <Font name="System Bold Italic" size="16.0" />
  47. </font></Label>
  48. <Label fx:id="winningRate" layoutX="64.0" layoutY="57.0" prefHeight="30.0" prefWidth="70.0">
  49. <font>
  50. <Font name="System Bold Italic" size="16.0" />
  51. </font></Label>
  52. <Label layoutX="291.0" layoutY="45.0" prefHeight="23.0" prefWidth="43.0" text="玩家:">
  53. <font>
  54. <Font name="Agency FB Bold" size="14.0" />
  55. </font>
  56. </Label>
  57. <Label layoutX="291.0" layoutY="337.0" prefHeight="23.0" prefWidth="43.0" text="电脑:">
  58. <font>
  59. <Font name="Agency FB Bold" size="14.0" />
  60. </font>
  61. </Label>
  62. <Line endX="-99.99996948242188" endY="326.9999694824219" layoutX="365.0" layoutY="73.0" startX="-99.99996948242188" startY="-83.66666412353516" />
  63. <Label fx:id="user" layoutX="335.0" layoutY="45.0" prefHeight="23.0" prefWidth="43.0" text="Label" />
  64. <Label fx:id="computer" layoutX="335.0" layoutY="337.0" prefHeight="23.0" prefWidth="43.0" text="Label" />
  65. <RPSView fx:id="View1" layoutX="400.0" layoutY="50.0" prefHeight="100.0" prefWidth="145.0" />
  66. <RPSView fx:id="View2" layoutX="400.0" layoutY="250.0" prefHeight="100.0" prefWidth="145.0" />
  67. <AnchorPane layoutX="370.0" layoutY="220.0" prefHeight="100.0" prefWidth="145.0" />
  68. </children>
  69. </AnchorPane>

2.otherConduct包

        由于这个包内部类的功能其实和上一篇博客类似,所以这里不再过多赘述!

有兴趣的读者可以前往我上一篇博客具体了解https://blog.csdn.net/Hc_Z05/article/details/138413416?spm=1001.2014.3001.5501

3. registerAndLogin包

(1)log包

LoginAction类:
  1. package myRPSGame.registerAndLogin.log;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.TextField;
  4. import myATM.otherConduct.DisplayAlert;
  5. import myATM.otherConduct.DisplayDialog;
  6. import myRPSGame.MainApp;
  7. import myRPSGame.gameView.GameAction;
  8. import myRPSGame.otherConduct2.DisPlayDialog;
  9. import myRPSGame.otherConduct2.GetCurrentTime;
  10. import myRPSGame.registerAndLogin.register.AccountList;
  11. import java.io.IOException;
  12. public class LoginAction
  13. {
  14. @FXML
  15. private TextField account;
  16. @FXML
  17. private TextField passport;
  18. //登录
  19. @FXML
  20. public void log() throws IOException
  21. {
  22. int n=AccountList.getTheAccount(account.getText());
  23. if(n!=-1)
  24. {
  25. if(AccountList.ifMatch(n,passport.getText()))
  26. {
  27. System.out.println("111");
  28. GameAction.setTheAccount(n);
  29. AccountList.getAccounts(n).getRecord().addRecord(STR."登录 时间:\{GetCurrentTime.getCurrentTime()}");
  30. MainApp.setRoot("gameView/View.fxml");
  31. }
  32. else DisplayAlert.display("输入的密码有误!");
  33. }
  34. }
  35. //忘记密码
  36. @FXML
  37. public void forgetPassport()
  38. {
  39. String s= DisPlayDialog.inputAccount();
  40. DisplayAlert.display("找回成功!",STR."账号:\{s}的密码为:",AccountList.getAccounts(AccountList.getTheAccount(s)).getPassport());
  41. }
  42. //注册
  43. @FXML
  44. public void register() throws IOException {
  45. MainApp.setRoot("registerAndLogin/register/RegisterView.fxml");
  46. }
  47. }
LoginView.fxml:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.layout.*?>
  4. <?import javafx.scene.text.*?>
  5. <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1"
  6. fx:controller="myRPSGame.registerAndLogin.log.LoginAction">
  7. <children>
  8. <Label layoutX="140.0" layoutY="80.0" prefHeight="35.0" prefWidth="60.0" text="账号:" textFill="#0c67ef">
  9. <font>
  10. <Font name="System Bold Italic" size="19.0" />
  11. </font></Label>
  12. <Label layoutX="140.0" layoutY="160.0" prefHeight="35.0" prefWidth="60.0" text="密码:" textFill="#08f6c7">
  13. <font>
  14. <Font name="System Bold Italic" size="20.0" />
  15. </font></Label>
  16. <TextField fx:id="account" layoutX="208.0" layoutY="86.0" />
  17. <TextField fx:id="passport" layoutX="208.0" layoutY="166.0" />
  18. <Button layoutX="401.0" layoutY="166.0" mnemonicParsing="false" onAction="#forgetPassport" text="忘记密码?">
  19. <font>
  20. <Font name="System Bold" size="13.0" />
  21. </font>
  22. </Button>
  23. <Button layoutX="266.0" layoutY="249.0" mnemonicParsing="false" onAction="#log" prefHeight="23.0" prefWidth="45.0" text="登录">
  24. <font>
  25. <Font name="System Bold" size="13.0" />
  26. </font>
  27. </Button>
  28. <Button layoutX="401.0" layoutY="86.0" mnemonicParsing="false" onAction="#register" prefHeight="23.0" prefWidth="70.0" text="注册账号">
  29. <font>
  30. <Font name="System Bold" size="13.0" />
  31. </font>
  32. </Button>
  33. <Label layoutY="2.0" prefHeight="23.0" prefWidth="180.0" text="Welcome to RPS game!" textFill="#e5bc07">
  34. <font>
  35. <Font name="System Bold Italic" size="14.0" />
  36. </font>
  37. </Label>
  38. <Label layoutX="200.0" layoutY="304.0" prefHeight="25.0" prefWidth="100.0" text="沉迷游戏伤身," textFill="#f41111">
  39. <font>
  40. <Font name="System Bold Italic" size="14.0" />
  41. </font></Label>
  42. <Label layoutX="300.0" layoutY="304.0" prefHeight="25.0" prefWidth="100.0" text="健康游戏益脑。" textFill="#f41111">
  43. <font>
  44. <Font name="System Bold Italic" size="14.0" />
  45. </font></Label>
  46. <Label layoutX="200.0" layoutY="333.0" prefHeight="25.0" prefWidth="100.0" text="珍惜现实世界," textFill="#5edb0a">
  47. <font>
  48. <Font name="System Bold Italic" size="14.0" />
  49. </font></Label>
  50. <Label layoutX="300.0" layoutY="333.0" prefHeight="25.0" prefWidth="100.0" text="多出门去走走。" textFill="#5edb0a">
  51. <font>
  52. <Font name="System Bold Italic" size="14.0" />
  53. </font></Label>
  54. </children>
  55. </AnchorPane>

(2)register包

Account类:
  1. package myRPSGame.registerAndLogin.register;
  2. import myATM.otherConduct.Record;
  3. public class Account
  4. {
  5. private String Accounts;
  6. private String Passport;
  7. private double winningRate=100;
  8. private String userName;
  9. private double playNumber=0;
  10. private double winningNUmber=0;
  11. private Record record=new Record();
  12. public String getAccounts() {
  13. return Accounts;
  14. }
  15. public void setAccounts(String accounts) {
  16. Accounts = accounts;
  17. }
  18. public String getPassport() {
  19. return Passport;
  20. }
  21. public void setPassport(String passport) {
  22. Passport = passport;
  23. }
  24. public double getWinningRate() {
  25. return winningRate;
  26. }
  27. //更新胜率
  28. public void setWinningRate() {
  29. this.winningRate=winningNUmber/playNumber*100;
  30. }
  31. public String getUserName() {
  32. return userName;
  33. }
  34. public void setUserName(String userName) {
  35. this.userName = userName;
  36. }
  37. public Record getRecord() {
  38. return record;
  39. }
  40. public void setRecord(Record record) {
  41. this.record = record;
  42. }
  43. public double getPlayNumber() {
  44. return playNumber;
  45. }
  46. public void setPlayNumber(double playNumber) {
  47. this.playNumber = playNumber;
  48. }
  49. public double getWinningNUmber() {
  50. return winningNUmber;
  51. }
  52. public void setWinningNUmber(double winningNUmber) {
  53. this.winningNUmber = winningNUmber;
  54. }
  55. public void setWinningRate(double winningRate) {
  56. this.winningRate = winningRate;
  57. }
  58. }
AccountList类:
  1. package myRPSGame.registerAndLogin.register;
  2. import myATM.otherConduct.DisplayAlert;
  3. import myRPSGame.otherConduct2.GetCurrentTime;
  4. public class AccountList {
  5. private static final int maxAccountNumber=1000;
  6. private static Account[] accounts=new Account[maxAccountNumber];
  7. private static int currentAccountNUmber=0;
  8. //添加账号
  9. public static void addAccount(String account,String passport,String username)
  10. {
  11. if(currentAccountNUmber<10)
  12. {
  13. accounts[currentAccountNUmber] = new Account();
  14. accounts[currentAccountNUmber].setAccounts(account);
  15. accounts[currentAccountNUmber].setPassport(passport);
  16. accounts[currentAccountNUmber].setUserName(username);
  17. accounts[currentAccountNUmber].getRecord().addRecord(STR."注册账号 注册时间:\{GetCurrentTime.getCurrentTime()}");
  18. currentAccountNUmber++;
  19. }
  20. else{
  21. DisplayAlert.display("当前的账号数已经有10个了!");
  22. }
  23. }
  24. //通过账号知道当前是第几个账号(已经减一)
  25. public static int getTheAccount(String account)
  26. {
  27. int i;
  28. for(i=0;i<currentAccountNUmber;i++)
  29. {
  30. if(accounts[i].getAccounts().equals(account))
  31. return i;
  32. }
  33. DisplayAlert.display("输入的账号不存在或者输入账号有误!");
  34. return -1;
  35. }
  36. //判断账号是否与密码匹配
  37. public static boolean ifMatch(int n,String passport) {
  38. return accounts[n].getPassport().equals(passport);
  39. }
  40. public static Account getAccounts(int n) {
  41. return accounts[n];
  42. }
  43. }
RegisterAction类:
  1. package myRPSGame.registerAndLogin.register;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.TextField;
  4. import myRPSGame.MainApp;
  5. import myRPSGame.otherConduct2.DisplayAlert;
  6. import myRPSGame.otherConduct2.GenerateRandomName;
  7. import java.io.IOException;
  8. public class RegisterAction
  9. {
  10. @FXML
  11. private TextField account;
  12. @FXML
  13. private TextField passport;
  14. @FXML
  15. private TextField username;
  16. //产生一个随机名
  17. @FXML
  18. public void RandomName()
  19. {
  20. username.setText(GenerateRandomName.randName());
  21. }
  22. //注册
  23. @FXML
  24. public void Register()
  25. {
  26. AccountList.addAccount(account.getText(),passport.getText(),username.getText());
  27. DisplayAlert.display("注册成功","欢迎新用户",username.getText());
  28. }
  29. //返回
  30. @FXML
  31. public void Return() throws IOException {
  32. MainApp.setRoot("registerAndLogin/log/LoginView.fxml");
  33. }
  34. }
RegisterView.fxml:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.layout.*?>
  4. <?import javafx.scene.text.*?>
  5. <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myRPSGame.registerAndLogin.register.RegisterAction">
  6. <children>
  7. <Label layoutX="130.0" layoutY="70.0" prefHeight="30.0" prefWidth="50.0" text="账号:">
  8. <font>
  9. <Font name="System Bold Italic" size="16.0" />
  10. </font></Label>
  11. <Label layoutX="130.0" layoutY="140.0" prefHeight="30.0" prefWidth="64.0" text="用户名:">
  12. <font>
  13. <Font name="System Bold Italic" size="16.0" />
  14. </font></Label>
  15. <Label layoutX="130.0" layoutY="210.0" prefHeight="30.0" prefWidth="50.0" text="密码:">
  16. <font>
  17. <Font name="System Bold Italic" size="16.0" />
  18. </font></Label>
  19. <TextField fx:id="account" layoutX="194.0" layoutY="73.0" />
  20. <TextField fx:id="username" layoutX="194.0" layoutY="144.0" />
  21. <TextField fx:id="passport" layoutX="194.0" layoutY="214.0" />
  22. <Button layoutX="368.0" layoutY="144.0" mnemonicParsing="false" onAction="#RandomName" text="随机" />
  23. <Label layoutX="325.0" layoutY="253.0" prefHeight="23.0" prefWidth="136.0" text="tips:不要忘记密码噢!" textFill="#eb0d0d">
  24. <font>
  25. <Font name="System Bold Italic" size="13.0" />
  26. </font>
  27. </Label>
  28. <Button layoutX="247.0" layoutY="290.0" mnemonicParsing="false" onAction="#Register" prefHeight="23.0" prefWidth="50.0" text="注册">
  29. <font>
  30. <Font name="System Bold Italic" size="13.0" />
  31. </font></Button>
  32. <Button layoutX="461.0" layoutY="313.0" mnemonicParsing="false" onAction="#Return" prefHeight="30.0" prefWidth="55.0" text="返回">
  33. <font>
  34. <Font name="System Bold" size="12.0" />
  35. </font>
  36. </Button>
  37. </children>
  38. </AnchorPane>

4.MainApp类

  1. package myRPSGame;
  2. import javafx.application.Application;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Scene;
  5. import javafx.scene.layout.AnchorPane;
  6. import javafx.stage.Stage;
  7. import java.io.IOException;
  8. import java.util.Objects;
  9. public class MainApp extends Application
  10. {
  11. private static Stage primaryStage;
  12. private static AnchorPane root;
  13. @Override
  14. public void start(Stage primaryStage) throws Exception
  15. {
  16. MainApp.primaryStage=primaryStage;
  17. MainApp.primaryStage.setTitle("剪刀石头布游戏 作者:cz Huang");
  18. initialize();
  19. primaryStage.show();
  20. }
  21. //设置根
  22. public static void setRoot(String rooter) throws IOException
  23. {
  24. root= FXMLLoader.load(Objects.requireNonNull(MainApp.class.getResource(rooter)));
  25. Scene scene=new Scene(root);
  26. primaryStage.setScene(scene);
  27. }
  28. //初始化
  29. public void initialize() throws IOException
  30. {
  31. root=FXMLLoader.load(Objects.requireNonNull(MainApp.class.getResource("registerAndLogin/log/LoginView.fxml")));
  32. Scene scene=new Scene(root);
  33. primaryStage.setScene(scene);
  34. }
  35. public static void main(String[] args){
  36. launch(args);
  37. }
  38. }

四:后话

        最近我在看红楼梦,我很喜欢那句“满纸荒唐言,谁解其中味”,正如我写的这些代码,其实在真正的高手眼中不过是一个刚学到新东西的毛头小子耍的三脚猫功夫,但我想,总会有读者能身有体会,在漫长的学习道路上,其中的酸甜苦辣,你知,我知。

       最后,感谢每一位读者!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/703386
推荐阅读
相关标签
  

闽ICP备14008679号