Taking Input/output in java
How to print on the GUI(Graphical user interface) in java
Taking input from console is not as straightforward as in C++. Initially we’ll study how to take input through GUI (by using JOPtionPane class).
The following program will take input (a number) through GUI and prints its square on the console as well on GUI.
1. import javax.swing.*;
2. public class InputOutputTest {
3. public static void main(String[] args) {
4. //takes input through GUI
5. String input = JOptionPane.showInputDialog("Enter number");
6. int number = Integer.parseInt(input);
7. int square = number * number;
8. //Display square on console
9. System.out.println("square:" + square);
10. //Display square on GUI
11. JOptionPane.showMessageDialog(null, "square:"+ square);
12. System.exit(0);
13. }
14. }
On line 1, swing package was imported because it contains the JOptionPane class that will be used for taking input from GUI and displaying output to GUI. It is similar to header classes of C++.
On line 5, showInputDialog method is called of JOptionPane class by passing string argument that will be displayed on GUI (dialog box). This method always returns back a String regardless of whatever you entered (int, float, double, char) in the input filed.
Our task is to print square of a number on console, so we first convert a string into a number by calling parseInt method of Integer wrapper class. This is what we done on line number 6.
Line 11 will display square on GUI (dialog box) by using showMessageDialog method of JOptionPane class. The first argument passed to this method is null and the second argument must be a String. Here we use string concatenation.
Line 12 is needed to return the control back to command prompt whenever we use
JoptionPane class.
How to print on the GUI(Graphical user interface) in java
Taking input from console is not as straightforward as in C++. Initially we’ll study how to take input through GUI (by using JOPtionPane class).
The following program will take input (a number) through GUI and prints its square on the console as well on GUI.
1. import javax.swing.*;
2. public class InputOutputTest {
3. public static void main(String[] args) {
4. //takes input through GUI
5. String input = JOptionPane.showInputDialog("Enter number");
6. int number = Integer.parseInt(input);
7. int square = number * number;
8. //Display square on console
9. System.out.println("square:" + square);
10. //Display square on GUI
11. JOptionPane.showMessageDialog(null, "square:"+ square);
12. System.exit(0);
13. }
14. }
On line 1, swing package was imported because it contains the JOptionPane class that will be used for taking input from GUI and displaying output to GUI. It is similar to header classes of C++.
On line 5, showInputDialog method is called of JOptionPane class by passing string argument that will be displayed on GUI (dialog box). This method always returns back a String regardless of whatever you entered (int, float, double, char) in the input filed.
Our task is to print square of a number on console, so we first convert a string into a number by calling parseInt method of Integer wrapper class. This is what we done on line number 6.
Line 11 will display square on GUI (dialog box) by using showMessageDialog method of JOptionPane class. The first argument passed to this method is null and the second argument must be a String. Here we use string concatenation.
Line 12 is needed to return the control back to command prompt whenever we use
JoptionPane class.
No comments:
Post a Comment