Link to home
Start Free TrialLog in
Avatar of J3n
J3n

asked on

help me! sorry about that

My code

===================================
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<APPLET CODE="CustomerDetail" WIDTH="500" HEIGHT="300"></APPLET> */
public class CustomerDetail extends Applet implements ActionListener
{
      Label lb1, lb2, lb3, lb4, lb5,lb6, lb7;
      TextField id, name, address, phone, email;
      Choice sex;
      Button bt1,bt2;
      GridBagLayout gb;
      GridBagConstraints gbc;
public void init()
{
      gb = new GridBagLayout();
      setLayout(gb);
      gbc= new GridBagConstraints();
      lb1 = new Label("Customer Detail");
      lb2 = new Label("Student ID");
      lb3 = new Label("Student Name");
      lb4 = new Label("Sex");
      lb5 = new Label("Address");
      lb6 = new Label("Phone");
      lb7 = new Label("Email");
      
      id = new TextField(20);
      name = new TextField(20);
      address = new TextField(20);
      phone = new TextField(20);
      email = new TextField(20);
      
      sex = new Choice();
      sex.addItem("Male");
      sex.addItem("FerMale");
      
      bt1 = new Button("Save");
      bt2 = new Button("Reset");
      
      addComponent(lb1,0,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb2,1,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(id,1,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb3,2,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(name,2,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb4,3,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(sex,3,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb5,4,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(address,4,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb6,5,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(phone,5,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb7,6,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(email,6,1,1,2,GridBagConstraints.HORIZONTAL);
      Panel p = new Panel();
      p.add(bt1);
      p.add(bt2);
      addComponent(p,7,0,1,1,GridBagConstraints.HORIZONTAL);
      gbc.anchor = GridBagConstraints.WEST;
      Font f = new Font("arial",Font.BOLD,13);
      lb2.setFont(f);
    lb3.setFont(f);
    lb4.setFont(f);
    lb5.setFont(f);
    lb6.setFont(f);
    lb7.setFont(f);
    bt1.setFont(f);
    bt2.setFont(f);
    Font f1 = new Font("arial",Font.BOLD, 16);
    lb1.setFont(f1);
    id.addActionListener(this);
    name.addActionListener(this);
    phone.addActionListener(this);
    email.addActionListener(this);
    bt1.addActionListener(this);
    bt2.addActionListener(this);  
}
public void addComponent(Component c, int row, int col, int nrow, int ncol, int fill)
{
      gbc.gridx = col;
      gbc.gridy = row;
      gbc.gridwidth = ncol;
      gbc.gridheight = nrow;
      gbc.fill = fill;
      gb.setConstraints(c,gbc);
      add(c);
}
public void actionPerformed (ActionEvent e)
{
        
      if(e.getSource() == bt2)
      {
            id.setText("");
            name.setText("");
            address.setText("");
            phone.setText("");
            email.setText("");
            showStatus("Reset");
      }
      if(e.getSource() == bt1)
      {
      if(id.getText().equals(""))
      {
      showStatus("Student ID field must be fill!!");
      id.requestFocus();
      }
    else if(name.getText().equals(""))
      {
      showStatus("Student Name field must be fill!!");
      name.requestFocus();
      }

}
}
}
==========================================
hix, i dunt know how to set: email must follow the pattern 'useid@company.com'
+ phone must be number
+ when user click on Save button all data must be save to "text.txt" file
Avatar of suprapto45
suprapto45
Flag of Singapore image

Hi,

1." email must follow the pattern 'useid@company.com'". i think that you can check it using instr method of String.
2. "phone must be number". I can give you some sample after this.
3. I will show you how to save data into text file.

Regards
Dave
Hi,

1.
String string = "userid@company.com";
b = string.indexOf("@") > 0;

if (b == false)
{
// Wrong email
}
else
{
// Do your logic here
}

URL:http://www.javaalmanac.com/egs/java.lang/HasSubstr.html

2.
You can use Integer.parseInt to check it.

try
{
int phone = Integer.parseInt("123123");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Phone must be number");
}

3.
http://www.devdaily.com/java/edu/pj/pj010017/index.shtml

I hope that helps.

Regards
Dave
Avatar of J3n
J3n

ASKER

thanks much
but i still error, can you write complete code for me
Thanks again
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Sorry.

             else if((email.getText()).indexOf('@') < 0)
             {                   
                  System.out.println("Email must have @!!");
             }

Regards
Dave
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial