0% found this document useful (0 votes)
27 views2 pages

Graphical Input and Output: Example 1

The document discusses graphical input and output in Java using the javax.swing package. It provides two methods for handling input and output: JOptionPane.showMessageDialog() displays a message in a dialog box, and JOptionPane.showInputDialog() displays a prompt and returns the user's input. Several examples are given that use these methods to display messages and get user input to then perform calculations or loops.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Graphical Input and Output: Example 1

The document discusses graphical input and output in Java using the javax.swing package. It provides two methods for handling input and output: JOptionPane.showMessageDialog() displays a message in a dialog box, and JOptionPane.showInputDialog() displays a prompt and returns the user's input. Several examples are given that use these methods to display messages and get user input to then perform calculations or loops.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Graphical Input and output

The javax.swing is package which provides two static methods for handling of
input and ouput operations
These methods available in JOptionPane class.
1. JOptionPane.showMessageDialog(null,String)
2. var = JOptionPane.showInputDialog(String)
Example 1

import javax.swing.*;
class output
{
public static void main(String args[])
{
JOptionPane.showMessageDialog(null,"HelloWorld");
}
}
Example 2

import javax.swing.*;
class input
{
public static void main(String args[])
{
String name=JOptionPane.showInputDialog("Enter your name");
JOptionPane.showMessageDialog(null,"Hello,"+name);
}
}
Example 3

import javax.swing.*;
class bignum
{
public static void main(String args[])
{
int a=Integer.parseInt(JOptionPane.showInputDialog("Enter first number"));
int b=Integer.parseInt(JOptionPane.showInputDialog("Enter first number"));
if(a>b)
JOptionPane.showMessageDialog(null,"Big is,"+a);
else
JOptionPane.showMessageDialog(null,"Big is,"+b);
}
}
Example 4 ---- Java program to print 1 to 10 numbers using for loop

class fordemo
{
public static void main(String args[])
{

for(int i=1;i<=10;i++)
System.out.println(i);

}
}

Example 5 --- Java program to print 1 to N numbers using for loop

import java.util.*;
class fordemo1
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter n");
n=sc.nextInt();
for(int i=1;i<=n;i++)
System.out.println(i);

}
}
Example 6 --- Java program to print 1 to N numbers using for loop
import javax.swing.*;
class fordemo2
{
public static void main(String args[])
{
int n;
String output="";
n=Integer.parseInt(JOptionPane.showInputDialog("Enter n"));
for(int i=1;i<=n;i++)
output=output+i+"\n";

JOptionPane.showMessageDialog(null,output);
}
}

You might also like