I recently started JavaFX, and have a small problem with a program I'm working on for school. The entire project is complete, besides a bug I can't figure out and a concern I have. The goal is to have a small box that checks to see if the addition problem is answered correctly, and can provide new problems.
The bug I have is this(I think the problem is in my handler for the New button, but can't find one.): After the initial problem, the numbers start being the same. As in, it might start: 7 + 2 but then will result to: 1+1, 2+2, 3+3, etc..
Another question I have is this: Given there is two Buttons, does that require more than one Handler, or is there a way to fit it into one? (I have not formally learned Lambda in this environment yet)
Here's the code:
package application;
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.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.util.Random;
import java.util.Date;
public class MathDrill extends Application
{
private Button checkButton, newButton;
private Label topNumLabel, botNumLabel, reply;
private TextField input;
//Check
class BCHCheck implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
Integer answer;
try
{
answer = Integer.parseInt(input.getText());
Integer topNum = Integer.parseInt(topNumLabel.getText()),
botNum = Integer.parseInt(botNumLabel.getText());
if(topNum + botNum == answer)
{
reply.setText("Correct!");
}
else
{
reply.setText("Incorrect!");
}
}
catch(NumberFormatException e)
{
reply.setText("Try again with an integer please.");
}
}
}
//New
class BCHNew implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
topNumLabel.setText(Integer.toString(rngInt()));
botNumLabel.setText(Integer.toString(rngInt()));
reply.setText("");
}
}
public static void main(String[] argv)
{
launch(argv);
}
public void start(Stage stage)
{
topNumLabel = new Label(Integer.toString(rngInt()));
botNumLabel = new Label(Integer.toString(rngInt()));
reply = new Label("");
Label words = new Label("Solve this:"),
plus = new Label("+"),
equalBar = new Label("-----");
input = new TextField();
GridPane problem = new GridPane();
problem.add(words, 0, 0);
problem.add(plus, 0, 4);
problem.add(topNumLabel,1, 1);
problem.add(botNumLabel, 1, 2);
problem.add(equalBar, 1, 3);
problem.add(input, 1, 4);
problem.add(reply, 1, 5);
checkButton = new Button("Check");
checkButton.setOnAction(new BCHCheck());
checkButton.setMinSize(100, 100);
newButton = new Button("New");
newButton.setOnAction(new BCHNew());
newButton.setMinSize(100, 100);
GridPane buttons = new GridPane();
buttons.add(checkButton, 0, 0);
buttons.add(newButton, 0, 1);
BorderPane bp = new BorderPane();
bp.setLeft(buttons);
bp.setCenter(problem);
Scene scene = new Scene(bp);
stage.setTitle("Math Drills");
stage.setScene(scene);
stage.setMinWidth(350);
stage.show();
}
/**
* Returns rng int value from 0-9
*
* @return rng int, [0, 9]
*/
private int rngInt()
{
return new Random(new Date().getTime()).nextInt(10);
}
}
Thanks for the help.
Aucun commentaire:
Enregistrer un commentaire