import java.awt.*;
import java.awt.event.*;
public class MyFrame {
public static void main(String[] args){
Button b;
ActionListener a = new MyActionListener();
Frame f = new Frame("Java Frame");
f.add(b = new Button("Java"), BorderLayout.NORTH);
b.setActionCommand("Java");
b.addActionListener(a);
f.add(b = new Button("VB"), BorderLayout.CENTER);
b.setActionCommand("VB");
b.addActionListener(a);
f.add(b = new Button(".Net"), BorderLayout.EAST);
b.setActionCommand(".Net");
b.addActionListener(a);
f.add(b = new Button("Software"), BorderLayout.WEST);
b.setActionCommand("Software");
b.addActionListener(a);
f.add(b = new Button("Exit"), BorderLayout.SOUTH);
b.setActionCommand("Exit");
b.addActionListener(a);
//f.pack();
f.setSize(300,200);
f.setVisible(true);
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
else if (s.equals("Java")) {
System.out.println("Java rocks.");
}
else if (s.equals("VB")) {
System.out.println("VB is old.");
}
else if (s.equals(".Net")) {
System.out.println(".Net is ok.");
}
else {
System.out.println(s + " clicked");
}
}
}
0 comments: