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

Create an applet for the central museum, 1) Central museum is one of the fa...

1) Central museum is one of the famous tourist places in london. They ask comments from every customer visiting them.  For that, they maintained one register to store visitor;s com

What is the comparable interface, What is the Comparable interface? Com...

What is the Comparable interface? Comparable interface is used to sort arrays and collections of objects using collections.sort() and java.utils. Objects of the class implement

Why class can''t extend more than one class, Why is an Interface be able to...

Why is an Interface be able to extend more than one Interface but a Class can't extend more than one Class? Basically Java doesn't permit multiple inheritances, so a Class is r

State the java programmer efficiency, State the Java Programmer Efficiency ...

State the Java Programmer Efficiency Faster Development Building an application in Java takes about 50% less time than in C or C++. So, faster time to market Java is said to

Explain the char data type in java, Explain the char data type in java? ...

Explain the char data type in java? A char is a single character in which a letter, a digit, a punctuation mark, a tab, a space or something same. A char literal is a single on

What are access specifiers available in java, What are Access Specifiers av...

What are Access Specifiers available in Java? Java has four access specifiers, which are enlisted below: Public: public methods, classes and fields can be accessed by ever

How can we involve images within a web page, How can we involve images with...

How can we involve images within a web page using HTML and Java script? Images can be included in HTML through employing tag, below is the format of

What is meant by semantic error, What is meant by semantic error? Occur...

What is meant by semantic error? Occur while a statement executes and has an effect not intended through the programmer and Frequently times occur only in unusual & infrequent

Explain the user defined exceptions, Explain the user defined Exceptions. ...

Explain the user defined Exceptions. User Defined Exceptions are the exceptions which are defined by user for specific purposed. This enables custom exceptions to be generated

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