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

Simple Tasks, Use a FOR loop and give me the largest number divisible by 7 ...

Use a FOR loop and give me the largest number divisible by 7 11 13 17 use the number from 1 to 10000 (or even 100000)

Student, need help in assignment

need help in assignment

Convert psd to android, Convert PSD to Android (UI only, no database) Pr...

Convert PSD to Android (UI only, no database) Project Description: I want someone highly proficient in Photoshop and Android SDK to change series of PSD files into live Andro

Explain traversing through a collector using iterator, Explain traversing t...

Explain traversing through a collector using Iterator. Ans. We can access each element in Collection by using Iterators regardless of how they are organized in collector. Ite

I want to develop a chat application for j2me, I want to develop a Chat app...

I want to develop a Chat application for j2me Project Description: We require a chat application for a community website. Skills required: Java, J2ME

While statement in java, The while statement define a loop that iterates as...

The while statement define a loop that iterates as long as condition remains true. In the following instance the control waits till the value of a text field becomes "go":

Develop a visual design in java, Player Ship Appearance: • Your ship mu...

Player Ship Appearance: • Your ship must be either a nice-looking sphere (left image above), or a nice-looking sphere with a "mouth" (middle image above), depending on variant.

Develop a 3d rolling alarm clock, Need to develop a 3D rolling alarm clock ...

Need to develop a 3D rolling alarm clock (Android) Project Description: I want an android app as 3D rolling alarm clock It should have sound effect when setting the alarm

Palindrome, A palindrome is a string that reads the same from both the ends...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Explain jakarta struts framework, Jakarta Struts is open source execution o...

Jakarta Struts is open source execution of MVC (Model-View-Controller) pattern for the development of web based applications. Jakarta Struts is robust architecture and can be used

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