I just registered because I have to submit my JavaFX application tomorrow morning February 24th and after trying many things, I could not get a solution for the last part of my application.
The application that I am developing is a game in which the user is one player, the PC is the other player, and the “machine” is an object that generates a random number.
If they guess the numbers that come, they add points to his score.
In the end, the first one wins (user or PC), which reaches 10 points.
Everything is still encoded, but when I try to finish the game, showing the winner’s message and leaving the game, I can’t find the code I have to write to exit the game and close the window 10 seconds after the message with the winner of the game is shown in the text area.
Could you help me tell me the code that I must impose on my application in order to close it automatically 10 seconds after the message about the winner appears, please ???
I need this for tomorrow February 24th at 07:30 GMT.
I have attached 4 files that I have: the main code of my JavaFX application, CSS for styles, the FXML file for the GUI and elements and the Java file that is used to run the application (I think), The application is called "Ramdoning". I am from Spain, so some words and comments are written in Spanish. Just go to the zone with ///////////////////////////////////. Where I want to know what I need to post.
Could you also tell me what I need to do to have one number specified in the combo box of the .fxml file, please ??? For example, to show the number 5 since the start of the program.
Here they are (files):
Estilos.css:
.fondo {
-fx-background-color: #0B610B;
}
.titulo {
-fx-font-family: Arial;
-fx-font-weight: Bold;
-fx-font-size: 30px;
-fx-text-align: centre;
-fx-text-fill: #66BBFC;
}
.texto {
-fx-font-family: Arial;
-fx-font-weight: Bold;
-fx-font-size: 15px;
-fx-text-fill: #F49A09;
}
FXMLDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="600.0" styleClass="fondo" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="ramdoning.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />
<Label layoutX="218.0" layoutY="38.0" styleClass="titulo" text="Ramdoning" visible="true">
<stylesheets>
<URL value="@Estilos.css" />
</stylesheets>
</Label>
<Label layoutX="44.0" layoutY="104.0" styleClass="texto" text="¿Que número aleatorio del 1 al 10 crees que la maquina va a generar?">
<stylesheets>
<URL value="@Estilos.css" />
</stylesheets>
</Label>
<TextField fx:id="tfAleatuser" layoutX="44.0" layoutY="136.0" prefWidth="200.0" text="1" />
<Label layoutX="44.0" layoutY="190.0" styleClass="texto" text="¿Qué número del 1 al 10 crees que va a elegir el PC?">
<stylesheets>
<URL value="@Estilos.css" />
</stylesheets>
</Label>
<ComboBox id="cb" fx:id="cbAleatpc" disable="false" layoutX="44.0" layoutY="223.0" visibleRowCount="10">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="1" />
<String fx:value="2" />
<String fx:value="3" />
<String fx:value="4" />
<String fx:value="5" />
<String fx:value="6" />
<String fx:value="7" />
<String fx:value="8" />
<String fx:value="9" />
<String fx:value="10" />
</FXCollections>
</items>
</ComboBox>
<Button fx:id="btAjugar" layoutX="437.0" layoutY="136.0" mnemonicParsing="false" onAction="#aJugar" prefHeight="54.0" prefWidth="117.0" text="¡A jugar!" />
<Label layoutX="44.0" layoutY="278.0" styleClass="texto" text="Resultado:">
<stylesheets>
<URL value="@Estilos.css" />
</stylesheets>
</Label>
<TextArea fx:id="taResultado" editable="true" layoutX="44.0" layoutY="307.0" opacity="0.72" prefHeight="355.0" prefWidth="510.0" wrapText="true" />
</children>
<stylesheets>
<URL value="@Estilos.css" />
</stylesheets>
</AnchorPane>
Ramdoning.java:
package ramdoning;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Ramdoning extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLDocumentController.java (Here I think I need to make changes. See /////////////////):
package ramdoning;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {
@FXML private TextField tfAleatuser;
@FXML private ComboBox cbAleatpc;
@FXML private TextArea taResultado;
int puntosusuario = 0;
int puntospc = 0;
@FXML
private void aJugar(ActionEvent event) {
String au = tfAleatuser.getText();
String auc = cbAleatpc.getValue().toString();
Random rnd = new Random();
int aleatorio = rnd.nextInt(10)+1;
String ag = Integer.toString(aleatorio);
int aleatorio_pc = rnd.nextInt(10)+1;
String ac = Integer.toString(aleatorio_pc);
int aleatorio_pc_u = rnd.nextInt(10)+1;
String acu = Integer.toString(aleatorio_pc_u);
taResultado.setText("Nº aleatorio elegido por el usuario: "+au+"\n\n");
taResultado.appendText("Nº aleatorio elegido por el PC: "+ac+"\n\n");
taResultado.appendText("El nº que el usuario piensa que el PC ha elegido es: "+auc+"\n\n");
taResultado.appendText("El nº que el PC piensa que el usuario ha elegido es: "+acu+"\n\n");
taResultado.appendText("Nº aleatorio generado: "+ag+"\n\n");
if(au.equals(ag))
{
puntosusuario++;
taResultado.appendText("¡¡¡El usuario ha adivinado el número aleatorio generado por la máquina!!!\n\n");
}
if(auc.equals(ac))
{
puntosusuario++;
taResultado.appendText("¡¡¡El usuario ha adivinado el número elegido por el PC!!!\n\n");
}
if(ac.equals(ag))
{
puntospc++;
taResultado.appendText("¡¡¡El PC ha adivinado el número aleatorio generado por la máquina!!!\n\n");
}
if(acu.equals(au))
{
puntospc++;
taResultado.appendText("¡¡¡El PC ha adivinado el número elegido por el usuario!!!\n\n");
}
String puntosu = Integer.toString(puntosusuario);
String puntosc = Integer.toString(puntospc);
taResultado.appendText("Marcador:\n"
+ "Usuario: "+puntosu+"\n"
+ "Ordenador: "+puntosc+"\n\n");
if(puntosusuario == 10)
{
taResultado.appendText("¡¡¡El usuario gana la partida!!!");
}
if(puntospc == 10)
{
taResultado.appendText("¡¡¡El PC gana la partida!!!");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}