Display a Message box in Java on Button Click.
import java.awt.*;
import java.awt.event.*;
public class messageBox extends Dialog
{
Button b1,b2;
messageBox(Frame fm,String lab)
{
super(fm,"Message",true);
setLayout(new GridLayout(2,1,0,0));
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));
p2.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));
p1.add(new Label(lab));
b1 = new Button("Ok");
b1.addActionListener(new B1());
p2.add(b1);
b2 = new Button("Cancel");
b2.addActionListener(new B1());
p2.add(b2);
add(p1);
add(p2);
setSize(350,125);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
class B1 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try
{
//Button ok = (Button)ae.getSource();
//String s = ok.getLabel();
if(ae.getSource() == b1 || ae.getSource() == b2)
{
System.out.println("Button Clicked");
dispose();
System.exit(0);
}
}
catch(Exception e){}
}
}
}
import java.awt.*;
import java.awt.event.*;
public class messageBox extends Dialog
{
Button b1,b2;
messageBox(Frame fm,String lab)
{
super(fm,"Message",true);
setLayout(new GridLayout(2,1,0,0));
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));
p2.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));
p1.add(new Label(lab));
b1 = new Button("Ok");
b1.addActionListener(new B1());
p2.add(b1);
b2 = new Button("Cancel");
b2.addActionListener(new B1());
p2.add(b2);
add(p1);
add(p2);
setSize(350,125);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
class B1 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try
{
//Button ok = (Button)ae.getSource();
//String s = ok.getLabel();
if(ae.getSource() == b1 || ae.getSource() == b2)
{
System.out.println("Button Clicked");
dispose();
System.exit(0);
}
}
catch(Exception e){}
}
}
}
No comments:
Post a Comment