Java sample(Message Dialog)


run
message1 message2

---(Source List)---

//Example07
import java.awt.*;
import java.awt.event.*;
public class Example07 extends Frame implements ActionListener{
  TextField tf01=new TextField("",5);
  TextField tf02=new TextField("",1);
  int pc=-1;
  //Main
  public static void main(String ar[]){
    Frame f=new Example07();
    f.setTitle("Message Dialog");
    f.setSize(640,400);
    f.setVisible(true);
  }
  //Button
  Example07(){
    setLayout(new FlowLayout());
    Button b01=new Button("Integer Check");
    Button b02=new Button("1 - 5 Check");
    b01.addActionListener(this);
    b02.addActionListener(this);
    add(tf01);
    add(b01);
    add(tf02);
    add(b02);
    addWindowListener(new WinAdapter());
  }
  //Close
  class WinAdapter extends WindowAdapter{
    public void windowClosing(WindowEvent we){System.exit(0);}
  }
  public void actionPerformed(ActionEvent ae){
    int i;
    String str;
    String strnum="0123456789";
    String str1to5="12345";
    if(ae.getActionCommand()=="Integer Check"){
      str=tf01.getText();
      if (str.length()==0){
        ErrDialog dia=new ErrDialog(this,"Error Message","Prease input something!",true);
        return;
      }
      for (i=0;i < str.length();i++){
        if (strnum.indexOf(str.substring(i,i+1))==-1){
          ErrDialog dia=new ErrDialog(this,"Error Message","Prease input Integer!",true);
          return;
        }
      }
    }
    if(ae.getActionCommand()=="1 - 5 Check"){
      str=tf02.getText();
      if (str.length()!=1){
        ErrDialog dia=new ErrDialog(this,"Error Message","Prease input 1 - 5!",true);
        return;
      }
      if (str1to5.indexOf(str)==-1){
        ErrDialog dia=new ErrDialog(this,"Error Message","Prease input 1 - 5!",true);
        return;
      }
    }
  }
}
class ErrDialog extends Dialog implements ActionListener {
  ErrDialog(Frame f,String title,String msg,boolean modal) {
    super(f,title,modal);
    setLayout(new FlowLayout());
    setSize(200,100);
    Label lbl = new Label(msg);
    add(lbl);
    Button btc = new Button("Close");
    add(btc);
    btc.addActionListener(this);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dispose();
  }
}