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

Describe the edit phase- java program development, Describe the Edit phase-...

Describe the Edit phase- Java Program Development Phase comprises editing a file. This is done with an editor program. Programmer types a java program using editor such as no

Create a new project in eclipse , Task 1 Create a new project in Eclips...

Task 1 Create a new project in Eclipse called Assignment 1. Within this project create a package called task01. 1/ Download the class Date (you must use this class - no

What is the role of hub in networks, What is the role of hub in networks? ...

What is the role of hub in networks? The network traffic controller - elements of convential computer networks. Hub is used to link the different computers within the network i

It, Implement a program that can compute your taxes (assume year 2012, as- ...

Implement a program that can compute your taxes (assume year 2012, as- sume a form 1040EZ). The form should ask for your ling status (single, married), income, deductions and cred

Program, Write a program called Power that displays the positive powers of ...

Write a program called Power that displays the positive powers of 2. When the user enters the exponent at a prompt, the program displays 2 to that power. The program halts when the

Program, Decode the Code Smugglers are becoming very smart day by day. Now ...

Decode the Code Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new techn

Non-final functions in java object class, Normal 0 false fals...

Normal 0 false false false EN-US X-NONE X-NONE

Session scope and Request scope, What is the difference among session scope...

What is the difference among session scope and request scope when saving formbean? Ans) When the scope is request, the values of formbean would be there for the current request.

Functions of specific codes, Please can you write some codes and their func...

Please can you write some codes and their functions too. At least 10! Please

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