Sunday, January 12, 2014

Java Swing Example Program 5

//Java Swing Example Program 5

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

public class ButtonEvent1 implements ActionListener{
private JFrame f;
private JButton b1,b2;
private JTextField jtf;

public ButtonEvent1(){
f    = new JFrame("Button Event");
b1 = new JButton("yes");
b2 = new JButton("no");
jtf  = new JTextField(20);
}

public void dispFrame(){
f.setLayout(new FlowLayout(FlowLayout.CENTER));
b1.addActi;onListener(this);
b2.addActionListener(this);
f.add(b1);
f.add(b2);
f.add(jtf);
f.setVisible(true);
f.setSize(300,300);
}

public void actionPerformed(ActionEvent ae){
String s=ae.getActionCommand();
if(s.equals("yes"))
jtf.setText("You pressed Yes");
if(s.equals("no"))
jtf.setText("You pressed No");
}


public static void main(String arg[]){
ButtonEvent1 bevent = new ButtonEvent1();
bevent.dispFrame();
}
}

No comments: