Sunday, January 12, 2014

Java Swing Example Program 2

//Java swing Example Program 2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;



public class MyMenu implements ActionListener{

JFrame jf;
JMenuBar mb;
JMenu m1,m2;
JMenuItem mi1,mi2,mi3,mi4;
JTextField jtf;


public MyMenu(){
jf = new JFrame("Menu  Example");
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
mi1 = new JMenuItem("Save");
mi2 = new JMenuItem("Exit");
mi3 = new JMenuItem("cut");
mi4=  new JMenuItem("Copy");
jtf = new JTextField(20);
}

public void display(){
m1.add(mi1);
m1.add(mi2);
m2.add(mi3);
m2.add(mi4);
mb.add(m1);
mb.add(m2);
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
jf.setJMenuBar(mb);
jf.add(jtf);
jf.setVisible(true);
jf.setSize(300,300);
}

public void actionPerformed(ActionEvent e){
jtf.setText(e.getActionCommand());
}

public static void main(String arg[]){
MyMenu mm = new MyMenu();
mm.display();
}
}

No comments: