Professional Documents
Culture Documents
JAVA Record KG College
JAVA Record KG College
JAVA Record KG College
RANDOM NUMBERS
Program:
import java.util.Random;
public static void main(String[] args) {
Random P=new Random();
int a[]=new int[25];
int i;
for(i=0;i<25;i++)
{ a[i]=P.nextInt(100); }
System.out.println("The Generated Random Numbers are");
for(i=0;i<25;i++)
{ System.out.print(a[i]+","); }
System.out.println("");
for(i=0;i<24;i++)
{
if(a[i]>a[i+1])
{
System.out.println(a[i]+" Is Greater than "+a[i+1]);
}
else if(a[i] < a[i + 1])
{
System.out.println(a[i]+" Is Lesser than "+a[i+1]);
}
Else
{
System.out.println(a[i]+" Is Eqal to "+a[i+1]);
}}}}
Output:
RATIONAL NUMBERS
Program:
import java.util.Scanner;
public class Prog_2 {
public static void main(String[] args)
{ int num1,num2;
Scanner input=new Scanner(System.in);
System.out.println("Enter the first value:");
num1=input.nextInt();
System.out.println("Enter the second value:");
num2=input.nextInt();
int num3=findGCD(num1,num2);
System.out.println("Common Factoris:"+num3);
num2=num2/num3;
num1=num1/num3;
System.out.println("Result is:"+num1+"/"+num2);
}
private static int findGCD(int n1,int n2)
{
if(n2==0)
{ returnn1; }
returnfindGCD(n2,n1%n2);
}
}
Output:
HEmpDet()
{
System.out.println(" ");
System.out.println(" Employee Program");
System.out.println(" ");
}
void get( )
{
{
System.out.println("Enter The EmpNo :");
this.empno=s.nextInt();
System.out.println("Enter The EmpName :");
this.ename=s.next();
System.out.println("Enter The Emp Bpay :");
Bpay=s.nextInt();
System.out.println("Enter The Emp Allowance:");
Allowance=s.nextInt();
System.out.println("Enter The EmpDedections:");
dedections=s.nextInt();
Gp=Bpay-dedections;
Netpay=Gp+Allowance;
System.out.println(" ");
}
}
String get(int a )
{
String fin; fin=Integer.toString(empno)+"/"+ename+"/"+Integer.toString(Bpay)
+"/"+Integer.toSt ring(Allowance)+"/"+Integer.toString(dedections)+"/"+Integer.toString(Gp)
+"/"+Inte ger.toString(Netpay);
return fin;
}
import javaprog.HEmpDet; //Package Declaration in FORM
public class Prog_3 extends javax.swing.JFrame {
String s1;
public Prog_3() {
HEmpDet s=new HEmpDet();
s.get();
s1=s.get(1);
String s2[]=s1.split("/");
initComponents();
jLabel1.setText(s2[0]); jLabel2.setText(s2[1]);
jLabel3.setText(s2[2]); jLabel13.setText(s2[3]);
jLabel14.setText(s2[4]); jLabel15.setText(s2[5]);
jLabel16.setText(s2[6]);
}
Output:
Employee Program
VEHICLE CLASS
Program:
Output:
package user_frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class User_frame {
public static void main(String[] args)
{ Scanner sc=new Scanner(System.in);
int x,y,w,h;
System.out.println("Enter the X position: ");
x=sc.nextInt();
System.out.println("Enter the Y position: ");
y=sc.nextInt();
System.out.println("Enter the Width: ");
w=sc.nextInt();
System.out.println("Enter the Height:");
h=sc.nextInt();
draw_frame(x,y,h,w);
}
Output:
MOUSE EVENTS
Program:
import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*
public class Prog_8 extends Applet implements MouseListener,MouseMotionListener
{
String txt=""; int
x=10,y=30;
public void init()
{ addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
txt="Mouse Clicked";
setForeground(Color.pink);
repaint();
}
public void mouseEntered(MouseEvent me)
{
txt="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
txt="Mouse Exited";
setForeground(Color.DARK_GRAY);
repaint();
}
public void mousePressed(MouseEvent me)
{
txt="Mouse Pressed";
setForeground(Color.BLUE);
repaint();
}
public void mouseMoved(MouseEvent me)
{
txt="Mouse Moved";
setForeground(Color.RED);
repaint();
}
public void mouseDragged(MouseEvent me)
{
txt="Mouse Dragged";
setForeground(Color.GREEN);
repaint();
}
public void mouseReleased(MouseEvent me)
{
txt="Mouse Released";
setForeground(Color.MAGENTA);
repaint();
}
public void paint(Graphics g)
{ g.drawString(txt,30,40);
showStatus("Mouse events Handling");}}
SIMPLE CALCULATOR
Program:
package calculator1;
import java.awt.*;
import java.awt.event.*;
public class Calculator1 implements ActionListener{
int c,n;
String s1,s2,s3,s4,s5;
Frame f;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
Panel p;
TextField tf;
GridLayout g;
Calculator1() {
f = new Frame("My calculator");
p = new Panel();
f.setLayout(new FlowLayout());
b1 = new Button("0");
b1.addActionListener(this);
b2 = new Button("1");
b2.addActionListener(this);
b3 = new Button("2");
b3.addActionListener(this);
b4 = new Button("3");
b4.addActionListener(this);
b5 = new Button("4");
b5.addActionListener(this);
b6 = new Button("5");
b6.addActionListener(this);
b7 = new Button("6");
b7.addActionListener(this);
b8 = new Button("7");
b8.addActionListener(this);
b9 = new Button("8");
b9.addActionListener(this);
b10 = new Button("9");
b10.addActionListener(this);
b11 = new Button("+");
b11.addActionListener(this);
b12 = new Button("-");
b12.addActionListener(this);
b13 = new Button("*");
b13.addActionListener(this);
b14 = new Button("/");
b14.addActionListener(this);
b15 = new Button("%");
b15.addActionListener(this);
b16 = new Button("=");
b16.addActionListener(this);
s3 = tf.getText();
s4 = "7";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b9) {
s3 = tf.getText();
s4 = "8";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b10){
s3 = tf.getText();
s4 = "9";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b11) {
s1 = tf.getText();
tf.setText("");
c=1; }
if(e.getSource()==b12) {
s1 = tf.getText();
tf.setText("");
c=2;}
if(e.getSource()==b13) {
s1 = tf.getText();
tf.setText("");
c=3;}
if(e.getSource()==b14) {
s1 = tf.getText();
tf.setText("");
c=4; }
if(e.getSource()==b15) {
s1 = tf.getText();
tf.setText("");
c=5; }
if(e.getSource()==b16) {
s2 = tf.getText();
if(c==1) {
n = Integer.parseInt(s1)+Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
else
if(c==2){
n = Integer.parseInt(s1)-Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
else
if(c==3) {
n = Integer.parseInt(s1)*Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
if(c==4){
try {
if(c==2){
n = Integer.parseInt(s1)-Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
else
if(c==3) {
n = Integer.parseInt(s1)*Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
if(c==4){
try {
int p=Integer.parseInt(s2);
if(p!=0){
n = Integer.parseInt(s1)/Integer.parseInt(s2);
tf.setText(String.valueOf(n));}
else
tf.setText("infinite");
}
catch(Exception i){}
}
if(c==5)
{
n = Integer.parseInt(s1)%Integer.parseInt(s2);
tf.setText(String.valueOf(n));
}
}
if(e.getSource()==b17)
{
tf.setText("");
}
}
public static void main(String[] abc)
{
Calculator1 v = new Calculator1();
}
}
Output:
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class Progr_12 extends javax.swing.JFrame {
public Progr_12() {
initComponents();
}
private void initComponents()
{ jPanel1 = new javax.swing.JPanel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jTextField6 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String filename= "D://StudentTxtF.txt";
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
out.println("Roll No: "+jTextField1.getText());
out.println("Name : "+jTextField2.getText());
out.println("DOB : "+jTextField3.getText());
out.println("Father : "+jTextField4.getText());
out.println("Dept : "+jTextField5.getText());
out.println("Phone : "+jTextField6.getText());
out.println(" ");
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
JOptionPane.showMessageDialog(null, "Success"); out.close();
} catch (IOException e)
{ JOptionPane.showMessageDialog(null, e);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{ System.exit(0);
}
Output:
Text File
ANIMATE IMAGES
Program:
import java.awt.*;
public class Progr_15 extends
Frame{ xPixel = 20;
static int yPixel = 20;
Image myImage;
public Progr_15()
{ try{
myImage = Toolkit.getDefaultToolkit().getImage("D:\\pictures\\img1.jpg");
}
catch(Exception e){}
setSize(800,600);
setVisible(true);
moveImage();
}
public void paint(Graphics g){
boolean b = g.drawImage(myImage, xPixel, yPixel, this);
}
void moveImage(){
for ( int i=0 ; i< 50000 ; i++){
//System.out.println("next set of Pixels " + xPixel);
for ( int j=1 ; j<5000000 ; j++)
{}
xPixel+=1;
yPixel +=1;
repaint();
}
}
public static void main(String[] args) {
// TODO code application logic here
Progr_15 me = new Progr_15();
}
}
Output:
CURRENCY CONVERSION
Program:
package currencyconverter;
import java.util.Scanner;
public class CurrencyConverter
{ public static final double
USD=65.00; public static final double
INR=1.53; public static void
main(String[] args) { double
ammount,usdResult,inrResult; int a=0;
Scanner sc=new Scanner(System.in);
do {
System.out.println("Enter the choice:");
System.out.println("1. USD to INR Conversion\n2. INR to USD Conversion\n3. Exit");
a=sc.nextInt();
switch(a)
{
case 1:
System.out.println("Enter the Ammount:");
ammount=sc.nextDouble();
System.out.println("Indian Value is:"+usdToInr(ammount)+"INR");
break;
case 2:
System.out.println("Enter the Ammount:");
ammount=sc.nextDouble();
System.out.println("Indian Value is:"+inrToUsd(ammount)+"INR");
break;
case 3:
break;
default:
System.out.println("Invalid Choice");
break;
}
}
while (a!=3);
}
private static double usdToInr(double ammount)
{ return (ammount*USD);
}
private static double inrToUsd(double ammount)
{ return ((ammount/100)*INR);
}
1. }
Output:
JDBC CONNECTION
Program:
Output:
PRODUCER CONSUMER
Program:
Output:
Consumer #1 got: 0
Producer #1 put: 0
Consumer #1 got: 1
Producer #1 put:1
Producer #1 put:2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9
TREE VIEWER
Program:
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
Output:
IMAGE VIEWER
Program:
import javax.swing.*;
public class Progr_14 extends javax.swing.JFrame
{ public Progr_14() {
initComponents();
}
private void initComponents()
{ jButton1 = new
javax.swing.JButton(); jLabel2 = new
javax.swing.JLabel(); jList1 = new
javax.swing.JList();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{ try{
if (jList1.getSelectedIndex() != -1)
{ String loc;
loc=jList1.getSelectedValue().toString();
ImageIcon icon1 = new ImageIcon("D:\\pictures\\"+loc+".jpg");
jLabel2.setIcon(icon1); }
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
public static void main(String args[])
{ public void run() {
new Progr_14().setVisible(true);
}
}
}
Output:
PALINDROME
Program:
import java.io.*;
import java.util.*;
for(String i:m)
{
String reverse = new StringBuffer(m[val]).reverse().toString();
if(i.equals(reverse)){
System.out.println(i);
cnt+=1;
}
val+=1;
}
}
System.out.println("No of Words:"+cnt);
}
catch (FileNotFoundException ex)
{ Logger.getLogger(Progr_18.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Output:
amma appa
malayalam
No of Words:3
SI UNITS
Program:
{
meters = (val*METERS);
}
else
{
meters = (val*KILOMETERS);
}
return meters;
}
public double fromMeters(double meters)
{
if(atoUnit.equals("in"))
{
converted = Math.round(meters*39.369923740457715);
}
else if(atoUnit.equals("ft"))
{
converted = Math.round(meters*3.280839895013123);
}
else if(atoUnit.equals("mi"))
{
converted = Math.round(meters*0.0006213688756330196);
}
else if(atoUnit.equals("mm"))
{
converted = Math.round(meters*1000);
}
else if(atoUnit.equals("cm"))
{
converted = Math.round(meters*100);
else if(atoUnit.equals("m"))
{
converted = Math.round(meters*1);;
}
else
{
converted = Math.round(meters*0.001);
}
return converted;
}
}
Reg. No: 2038M0066
KGiSL-IIM
Output:
Convert from:
in
Convert to:
ft
Value:
56
56.0 in=5.0 ft
SHAPES
Program:
package fb;
import java.awt.*;
importjavax.swing.*;
public class fb extends
javax.swing.JFrame{ static fb FS=newfb();
String shp,clr;
public fb()
{ initComponents
();
}
@SuppressWarnings("unchecked")
private void initComponents() {
mnuBr_Shps = new javax.swing.JMenuBar();
mnu_Shp = new javax.swing.JMenu();
mnu_ShpRect = new javax.swing.JMenuItem();
mnu_ShpOvl = new javax.swing.JMenuItem();
mnu_ShpArc = new javax.swing.JMenuItem();
mnu_FldShp = new javax.swing.JMenu();
mnu_FldShpRect = new javax.swing.JMenuItem();
mnu_FldShpOvl = new javax.swing.JMenuItem();
mnu_FldShpArc = new javax.swing.JMenuItem();
mnu_Clr = new javax.swing.JMenu();
mnu_ClrRd = new javax.swing.JMenuItem();
mnu_ClrGrn = new javax.swing.JMenuItem();
mnu_ClrBlu = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mnu_Shp.setText("Shapes"); mnu_ShpRect.setText("Rectangle");
mnu_ShpRect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sbmnu_ShpActionPerformed(evt);
}
});
mnu_Shp.add(mnu_ShpRect);
mnu_ShpOvl.setText("Oval");
mnu_ShpOvl.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
});
mnu_Shp.add(mnu_ShpOvl);
mnu_ShpArc.setText("Arc");
mnu_ShpArc.addActionListener(new java.awt.event.ActionListener() { public
voidactionPerformed(java.awt.event.ActionEvent evt)
{
sbmnu_ShpActionPerformed(evt);
}
}); mnu_Shp.add(mnu_ShpArc);
mnuBr_Shps.add(mnu_Shp);
mnu_FldShp.setText("Filled Shapes");
mnu_FldShpRect.setText("Filled Rectangle");
mnu_FldShpRect.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
}); mnu_FldShp.add(mnu_FldShpRect);
mnu_FldShpOvl.setText("Filled Oval");
mnu_FldShpOvl.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
}); mnu_FldShp.add(mnu_FldShpOvl);
mnu_FldShpArc.setText("Filled Arc");
mnu_FldShpArc.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
});
mnu_FldShp.add(mnu_FldShpArc);
mnuBr_Shps.add(mnu_FldShp);
mnu_Clr.setText("Colors");
mnu_ClrRd.setText("Red");
mnu_ClrRd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{ mnu_ClrActionPerformed(evt);
}
}); mnu_Clr.add(mnu_ClrRd);
mnu_ClrGrn.setText("Green");
mnu_ClrGrn.setToolTipText("");
mnu_ClrGrn.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ mnu_ClrActionPerformed(evt);
}
});
mnu_Clr.add(mnu_ClrGrn);
mnu_ClrBlu.setText("Blue");
mnu_ClrBlu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{ mnu_ClrActionPerformed(evt);
}
});
mnu_Clr.add(mnu_ClrBlu);
mnuBr_Shps.add(mnu_Clr);
setJMenuBar(mnuBr_Shps);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayo
ut.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
pack();
}
private void sbmnu_ShpActionPerformed(java.awt.event.ActionEvent evt)
{ Graphics g =getGraphics();
g.clearRect(0,50,getWidth(), getHeight());
if(clr=="red")
{
g.setColor(Color.red);
}
else if(clr=="green")
{ g.setColor(Color.green);
}
else if(clr=="blue")
{ g.setColor(Color.blue);
}
if(evt.getSource()==mnu_ShpRect)
{
g.drawRect(100, 120, 100, 100);
}
else if(evt.getSource()==mnu_ShpOvl)
{ g.drawOval(100,120,100,100);
}
else if(evt.getSource()==mnu_ShpArc)
{ g.drawArc(100, 120, 100, 100, 90,
120);
}
else if(evt.getSource()==mnu_FldShpRect)
{ g.fillRect(100,120,100,100);
}
else if(evt.getSource()==mnu_FldShpOvl){
g.fillOval(100,120,100,100);
}
else if(evt.getSource()==mnu_FldShpArc)
{ g.fillArc(100, 120, 100, 100, 90, 120);
}
}
private void mnu_ClrActionPerformed(java.awt.event.ActionEvent evt)
{ if(evt.getSource()==mnu_ClrRd)
{
clr="red";
}
else if(evt.getSource()==mnu_ClrGrn)
{ clr="green";
}
else if(evt.getSource()==mnu_ClrBlu)
{ clr="blue";
}
}
public static void main(String args[])
{ try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels())
{ if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
}
SwingUtilities.invokeLater(() -> { FS.setVisible(true);
FS.setBounds(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]),Inte
ger.parseInt(args[3]));
});
}
Output:
DATE CLASS
Program:
Output:
import java.io.PipedReader;
import java.io.PipedWriter;
{
return out1;
}
public void run()
{
Thread t=Thread.currentThread();
t.setName("Prime");
System.out.println(t.getName() +" Thread Started...");
int prime=1;
while(true)
{
try
{
if(prime>100000)
{
out1.close();
break;
}
if(isPrime(prime))
out1.write(prime);
prime++; sleep(0);
}
catch(Exception e)
{
System.out.println(t.getName()+" Thread exiting...");
System.exit(0);
}
}
}
public boolean isPrime(int n)
{
int m=(int)Math.round(Math.sqrt(n));
if(n==1||n==2)
returntrue;
for(int i=2;i<=m;i++) if(n
%i==0)
return false;
returntrue;
}
}
class MultiThreadDemo
{
public static void main(String[] args)throws Exception
{
Thread t=Thread.currentThread();
t.setName("Main"); System.out.println(t.getName()
+" ThreadStarted..."); Progr_17
fibObj=newProgr_17();
Prime primeObj=new Prime();
PipedReader pr=new PipedReader(fibObj.getPipedWriter());
PipedReader pr1=new PipedReader(primeObj.getPipedWriter());
fibObj.start();
primeObj.start();
int fib=pr.read(),prm=pr1.read();
System.out.println("The numbers common to PRIME and FIBONACCI:");
while((fib!=-1)&&(prm!=-1))
{
while(prm<=fib)
{ if(fib==prm)
System.out.println(prm);
prm=pr1.read();
}
fib=pr.read();
}
System.out.println(t.getName()+ " Thread exiting...");
}
}
Output:
NOTEPAD EDITOR
Program:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyNotepade extends Frame implements ActionListener
{
private TextArea txta_show;
private Menu file;
private MenuItem New,Open,Save,Exit;
private MenuBar mb=new MenuBar();
public MyNotepade()
{
file=new Menu("File");
New=new MenuItem("New");
Open=new MenuItem("Open");
Save=new MenuItem("Save");
Exit=new MenuItem("Exit");
file.add(New);
file.add(Open);
file.add(Save);
file.add(Exit);
mb.add(file);
New.addActionListener(this);
Open.addActionListener(this);
Save.addActionListener(this);
Exit.addActionListener(this);
setTitle("Notpad");
setSize(400,400);
setLocation(100,100);
setMenuBar(mb);
txta_show=new TextArea();
add(txta_show);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
} });
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==New)
{
txta_show.setText(" ");}
else if(ae.getSource()==Open)
{
try{
FileDialog fd=new FileDialog(this,"Open File",FileDialog.LOAD);
fd.setVisible(true);
String dir=fd.getDirectory();
String fname=fd.getFile();
FileInputStream fis=new FileInputStream(dir+fname);
DataInputStream dis=new DataInputStream(fis);
String str=" ",msg=" "; while((str=dis.readLine())!
=null)
{
msg=msg+str;
msg+="\n";
}
txta_show.setText(msg);
dis.close();
}
catch(Exception e){}
}
else if(ae.getSource()==Save)
{
try
{
FileDialog fd=new FileDialog(this,"Save File",FileDialog.SAVE);
fd.setVisible(true);
String txt=txta_show.getText();
String dir=fd.getDirectory();
String fname=fd.getFile();
FileOutputStream fos=new FileOutputStream(dir+fname);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeBytes(txt);
dos.close();
}
catch(Exception e){}
}
else if(ae.getSource()==Exit)
{
System.exit(0);
}}}
class Notepad
{
public static void main(String []args)
{
new MyNotepade().setVisible(true);
}}
Output: