Wednesday, 30 January 2013

GUI Example of Whole AWT Components with its event in JAVA


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

class AWTCmp extends Frame implements WindowListener, ActionListener,MouseListener,MouseMotionListener,KeyListener,AdjustmentListener,TextListener,ItemListener
{
private static final SimpleDateFormat clockFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Panel p_header = new Panel();
Panel p_footer = new Panel();
Panel p_button = new Panel();
Panel p_main = new Panel();
Panel p_msg = new Panel();

Button b_add = new Button("ADD");
        Button b_del = new Button("DELETE");
Button b_update = new Button("UPDATE");
Button b_save = new Button("SAVE");
Button b_first = new Button("<<");
Button b_pre = new Button("<");
Button b_next = new Button(">");
  Button b_last = new Button(">>");
Button b_view = new Button("VIEW");
Button b_close = new Button("CLOSE");


Label l_header = new Label("*** EMPLOYEE INFORMATION ***");

Label l_no = new Label("NO");
Label l_name = new Label("NAME");
Label l_add = new Label("ADDRESS");
Label l_state = new Label("STATE");
Label l_phone = new Label("PHONE");
Label l_occu = new Label("OCCUPATION");

TextField t_no = new TextField();
TextField t_name = new TextField();
TextArea t_add = new TextArea();

Choice ch_state = new Choice();

TextField t_phone = new TextField();

Panel p_occu = new Panel();
Checkbox jbx1 = new Checkbox("Business");
Checkbox jbx2 = new Checkbox("Private Sector");
Checkbox jbx3 = new Checkbox("Goverment Employee");
Checkbox jbx4 = new Checkbox("NGO");


Font f_header = new Font("Cambria",Font.BOLD,28);
Font f_button = new Font("Britannic Bold",Font.BOLD,14);
Font f_msg = new Font("Cambria",Font.BOLD,20);

Label l_msg = new Label("All Message are Display Here.....................");
Label l_date = new Label("Current Date");
Label l_scroll = new Label("Current Scroll Poition");

MenuBar mb = new MenuBar();

Menu m_file = new Menu("FILE");
Menu m_edit = new Menu("EDIT");
Menu m_format = new Menu("FORMAT");
Menu m_view = new Menu("VIEW");
Menu m_help = new Menu("HELP");

MenuItem mi_new = new MenuItem("NEW");
MenuShortcut msc = new MenuShortcut(KeyEvent.VK_O);
MenuItem mi_open = new MenuItem("OPEN",msc);

MenuItem mi_save = new MenuItem("SAVE");
MenuItem mi_print = new MenuItem("PRINT");
MenuItem mi_exit = new MenuItem("EXIT");

MenuItem mi_cut = new MenuItem("CUT");
MenuItem mi_copy = new MenuItem("COPY");
MenuItem mi_paste = new MenuItem("PASTE");
MenuItem mi_undo = new MenuItem("UNDO");

MenuItem mi_font = new MenuItem("FONT");
MenuItem mi_word = new MenuItem("WORD WRAP");

MenuItem mi_status = new MenuItem("STATUS BAR");
MenuItem mi_clock = new MenuItem("CLOCK");

MenuItem mi_topics = new MenuItem("TOPICS");
MenuItem mi_about = new MenuItem("ABOUT.....");

Scrollbar sb = new Scrollbar(Scrollbar.VERTICAL);

AWTCmp(String s)
{
super(s);
setBounds(0,0,1250,700);

performEventListener();

setComponent();

setInitialText();

startClockThread();

/*addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});*/

}
void performEventListener()
{
this.addWindowListener(this);
l_msg.addMouseListener(this);
l_msg.addMouseMotionListener(this);
t_no.addKeyListener(this);
t_no.requestFocus();

b_close.addActionListener(this);
b_add.addActionListener(this);
b_view.addActionListener(this);


jbx1.addItemListener(this);
jbx2.addItemListener(this);
jbx3.addItemListener(this);
jbx4.addItemListener(this);

t_name.addTextListener(this);

sb.addAdjustmentListener(this);

mi_exit.addActionListener(this);
mi_open.addActionListener(this);

}
void setInitialText()
{
t_no.setText("10001");
t_name.setText("VANDANA VAISHNAV");
t_add.setText("MAHAVIR NAGAR, HIMATNAGAR");
t_phone.setText("9429477170");
}
void clearText()
{
t_no.setText("");
t_name.setText("");
t_add.setText("");
t_phone.setText("");
}

void openFile()
{
FileDialog fd = new FileDialog(this,"Open File", FileDialog.LOAD);
fd.show();
if(fd.getFile() == null)
return;
l_msg.setText(fd.getDirectory() + fd.getFile() + " is Selected");
}

void startClockThread()
{
Runnable clockTicker = new Runnable()
{
public void run()
{
Date date = new Date();
while(true)
{
l_date.setText(clockFormat.format(date));
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
date.setTime(System.currentTimeMillis());
}
}
};
Thread thread = new Thread(clockTicker);
thread.setDaemon(true);
thread.start();
}

void setComponent()
{
// ADD IN FRAME

this.add(p_header,BorderLayout.NORTH);
this.add(p_footer,BorderLayout.SOUTH);
this.add(p_main,BorderLayout.CENTER);
this.add(sb,BorderLayout.EAST);

// ADD IN p_header
p_header.setBackground(Color.GRAY);
p_header.setFont(f_header);
p_header.add(l_header,BorderLayout.CENTER);

// add in p_footer

l_msg.setFont(f_msg);
l_date.setFont(f_button);
l_scroll.setFont(f_button);
p_msg.setLayout(new GridLayout(1,3));
p_msg.add(l_date);
p_msg.add(l_msg);
p_msg.add(l_scroll);

p_footer.setLayout(new GridLayout(2,1));

p_footer.add(p_msg);
p_footer.add(p_button);

p_button.setFont(f_button);
p_button.setLayout(new GridLayout(2,5));

p_button.add(b_add);
p_button.add(b_del);
p_button.add(b_update);
p_button.add(b_save);
p_button.add(b_view);
p_button.add(b_first);
p_button.add(b_pre);
p_button.add(b_next);
p_button.add(b_last);
p_button.add(b_close);


// add in p_occu

p_occu.setLayout(new GridLayout(2,2));
p_occu.add(jbx1);
p_occu.add(jbx2);
p_occu.add(jbx3);
p_occu.add(jbx4);

// add in p_main

p_main.setFont(f_button);
p_main.setLayout(new GridLayout(6,2));

p_main.add(l_no);
p_main.add(t_no);
p_main.add(l_name);
p_main.add(t_name);
p_main.add(l_add);
p_main.add(t_add);
p_main.add(l_state);

// add into Choice
ch_state.add("GUJARAT");
    ch_state.add("UTTAR PRADESH");
    ch_state.add("MAHARASTRA");
    ch_state.add("MADAYA PADESH");

p_main.add(ch_state);
p_main.add(l_phone);
p_main.add( t_phone);
p_main.add(l_occu);
p_main.add(p_occu);

//add in menu

m_file.add(mi_new);
m_file.add(mi_open);
m_file.add(mi_save);
m_file.add(mi_print);
m_file.add(mi_exit);

m_edit.add(mi_cut);
m_edit.add(mi_copy);
m_edit.add(mi_paste);
m_edit.add(mi_undo);

m_format.add(mi_font);
m_format.add(mi_word);

m_view.add(mi_status);
m_view.add(mi_clock);

m_help.add(mi_topics);
m_help.add(mi_about);

mb.add(m_file);
mb.add(m_edit);
mb.add(m_format);
mb.add(m_view);
mb.add(m_help);

this.setMenuBar(mb);
}


// Window Methods

public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void windowClosed(WindowEvent we)
{
;
}
public void windowOpened(WindowEvent we)
{
l_msg.setText("Window is Opened");
}
public void windowDeactivated(WindowEvent we)
{
l_msg.setText("Window is Deactive");
}
public void windowActivated(WindowEvent we)
{
l_msg.setText("Window is Active");
}
public void windowDeiconified(WindowEvent we)
{
l_msg.setText("Window is Deiconified / Maximized");
}
public void windowIconified(WindowEvent we)
{
l_msg.setText("Window is Iconified / Minimized");
}

// Action Methods

public void actionPerformed(ActionEvent ae)
{
String actcmd = ae.getActionCommand();

if( actcmd.equals("CLOSE") || actcmd.equals("EXIT"))
{
System.exit(0);
}
if( actcmd.equals("VIEW"))
{
setInitialText();
}
if( actcmd.equals("ADD"))
{
clearText();
}
if( actcmd.equals("OPEN"))
{
openFile();
}

}
// Mouse Method

public void mouseClicked(MouseEvent me)
{
l_msg.setText(" *** MOUSE IS CLICKED ***");
}
public void mouseEntered(MouseEvent me)
{
l_msg.setText(" *** MOUSE IS ENTERED ***");
}
public void mouseExited(MouseEvent me)
{
l_msg.setText(" *** MOUSE IS EXIT ***");
}
public void mousePressed(MouseEvent me)
{
l_msg.setText(" *** MOUSE  IS PRESSED ***");
}
public void mouseReleased(MouseEvent me)
{
l_msg.setText(" *** MOUSE RELEASED ***");
}
public void mouseMoved(MouseEvent me)
{
l_msg.setText(" *** MOUSE MOVED ***");
}
public void mouseDragged(MouseEvent me)
{
l_msg.setText(" *** MOUSE DRAGGED ***");
}

//Key Method

public void keyTyped(KeyEvent ke)
{
l_msg.setText(" Key Typed..........." + ke.getKeyChar());
}
public void keyPressed(KeyEvent ke)
{
String keyname = " ";
int keycode = ke.getKeyCode();

if(keycode == ke.VK_F1)
keyname = "F1";
else if(keycode == ke.VK_SHIFT)
keyname = "SHIFT";
l_msg.setText(" Key Pressed..........." + keyname);
}
public void keyReleased(KeyEvent ke)
{
l_msg.setText(" KeyRelesed...........");
}

//Adjustment Methods

public void adjustmentValueChanged(AdjustmentEvent ae)
{
l_scroll.setText(String.valueOf(ae.getValue()));
    }

//Text Methods

public void textValueChanged(TextEvent te)
{
l_msg.setText("*** Text Event is Performed ***");
    }

//Item Methods

public void itemStateChanged(ItemEvent ie)
{
// Checkbox cb = ( Checkbox )  ie.getItem();
 int state = ie.getStateChange();
 if (state == ItemEvent.SELECTED)
   l_msg.setText(String.valueOf(ie.getItem()) + " is Selected");
   else
    l_msg.setText(String.valueOf(ie.getItem()) + "  is Cleared");
    }
}
class  AWTComponent
{
public static void main(String args[])
{
AWTCmp awtcmp = new AWTCmp("**** AWTComponent ****");
awtcmp.setVisible(true);
}

}

OUTPUT





No comments:

Post a Comment