Question
Hi ! Need help converting this code into GUI. It should have one textfield for user input ( ex 1234) , and one for displaying
Hi ! Need help converting this code into GUI. It should have one textfield for user input ( ex 1234) , and one for displaying the reversed number ( as Int). Aswell as adding a button("Reverse") to reverse the input number and to display it in second textfield. The recursive method in the GUI program should have the following signature public int reverseInteger(int value) , and validate input is a positive integer. The layout should be like this :
Below is the code I need to be "converted" to GUI type code, as well as an attempt for the GUI code, and the layout for buttons,textfields etc. I am having trouble implementing the reverseInteger code into GUI.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);// Create scanner for input
System.out.print("Enter integer to be reversed,please,Sir: ");
int number = input.nextInt();
System.out.print("The reversed number is ");
// Instanceof
ReverseIntFirst inst = new ReverseIntFirst();
inst.reverseInteger(number);
}
public void reverseInteger(int value) {
if (value != 0) { // Basis
System.out.print(value % 10);
value = value / 10;
reverseInteger(value); //Recursive
}
}
}
And here is my GUI Code:
public class ReversedInteger extends Application{
double paneWidth = 500;
double paneHeight = 250;
@Override
public void start(Stage primaryStage) {
// Textfield for entering user input, example 43223
TextField tfOrgNumber = new TextField();
tfOrgNumber.setPromptText("Enter number");
tfOrgNumber.getText();
// Textfield to display the reversed integer from user input
TextField tfRevNumber = new TextField();
tfRevNumber.setPromptText("Reversed number");
// Alignment for textfields
tfOrgNumber.setAlignment(Pos.BOTTOM_RIGHT);
tfRevNumber.setAlignment(Pos.BOTTOM_RIGHT);
// Create button
Button btReverse = new Button("Reverse");
// New pane, adding tf`s and bt
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.add(new Label("Your number"), 0, 0);
pane.add(tfOrgNumber, 1, 0);
pane.add(new Label("Your reversed number"), 0, 1);
pane.add(tfRevNumber, 1, 1);//*
pane.add(btReverse,1,2);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, paneWidth, paneHeight);
primaryStage.setTitle("Reverse Integer"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
// Eventhandler for button
btReverse.setOnAction( e-> }
/*public void reverseInteger(int value) { //
// I know i can use parseInt to fetch int from textfield , but not really how to do it.
if (value != 0) { // Basis
System.out.print(value % 10);
value = value / 10;
reverseInteger(value); //Recursive
}*/
// Integer.parseInt(tfOrgNumber.getText());
public static void main(String[] args) { // Main method
System.out.println("");
launch(args);
}}
Reverse Integer Your number Enter number Your reversed number Reversed number ReverseStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started