You are on page 1of 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