Illustrate the method to print on the GUI, JAVA Programming

Assignment Help:

Illustrate the method to print on the GUI

Example Code: Taking Input / Output

So far, we learned how to print something on console. Now time has come to learn how to print on the GUI. Taking input from console isn't as straightforward as in C++. At first we'll study how to take input through GUI (by employing 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 since it comprise JOptionPane class which will be used for taking input from GUI and displaying output to GUI. It's similar to header classes of C++.

On line 5, showInputDialog method is called of JOptionPane class by passing string argument which would be displayed on GUI (dialog box). This method always returns back a String regardless of whatever you entered (int, float, double, char) in input filed.

Our task is to print square of a number on console thus 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 would display square on GUI (dialog box) by using showMessageDialog method of JOptionPane class. First argument passed to this method is null and second argument should be a String. Here we use string concatenation.

Line 12 is required to return control back to command prompt whenever we use JoptionPane class.

 

 


Related Discussions:- Illustrate the method to print on the GUI

Explain the relational operator precedence, Explain the Relational Operator...

Explain the Relational Operator Precedence ? Whenever a new operator is introduced you have to ask yourself whereas it fits in the precedence tree. If you seem back at the ins

Legal responsibility of nurses, As registered nurses, we often wonder "am I...

As registered nurses, we often wonder "am I responsible for the L.P.N's and the C.N.A. assigned on my floor.  Will I be blamed if someone makes an error or if someone gets hurt?  N

Develop an android app for me, Project Description: I need someone to pr...

Project Description: I need someone to prepare an android app for me and provide it to me asap. The app details to be provided on pm Skills required: Java, Android

Help, Are you real? I dont want to get help from a computer

Are you real? I dont want to get help from a computer

Explain the methods of java.lang.object, Explain The Methods of java.lang.O...

Explain The Methods of java.lang.Object? java.lang.Object gives a number of methods that are general to all objects. toString() is the most general such method. Because the def

Vigenere Cipher decryption unknown key, I need help writing this code eithe...

I need help writing this code either in java or in python. basically the steps are described here in this youtube video or">https://www.youtube.com/watch?v=LaWp_Kq0cKs or this is

Method overriding, how to write method overriding and example programe

how to write method overriding and example programe

How to initialize array in java, How to initialize array in java ? Indi...

How to initialize array in java ? Individual elements of an array are referenced through the array name and through an integer that represents their position in the array. The

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd