import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MeFrame extends JFrame
{
MeFrame()
{
getContentPane().add(new hiPanel(this));
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
JFrame f=(JFrame)we.getSource();
int result;
result=JOptionPane.showConfirmDialog(null,"现在要退出?","消息",JOptionPane.OK_CANCEL_OPTION);
if(result==0)
{
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else
{
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
}
);
}
public static void main(String a[])
{
new MeFrame();
}
}class NameLengthException extends Exception
{
String message;
NameLengthException(int min,int max)
{
message="名字必须在"+String.valueOf(min)+"和"+String.valueOf(max)+"之间";
}
public String getMessage()
{
return message;
}
}
class PasswordLengthException extends Exception
{
String message;
PasswordLengthException(int min,int max)
{
message="密码必须在"+String.valueOf(min)+"和"+String.valueOf(max)+"之间";
}
public String getMessage()
{
return message;
}
}class CustInfo
{
String name=new String("");
String pwd=new String("");
String gender=new String("");
String phone=new String("");
String email=new String("");
boolean validate() throws NameLengthException,PasswordLengthException
{
if(name.length()>8||name.length()<12)
{
throw new NameLengthException(0,8);
// JOptionPane.showMessageDialog(null,"Name is error");
// return false;
}
if(pwd.length()>8||pwd.length()<12)
{
throw new PasswordLengthException(0,8);
/* JOptionPane.showMessageDialog(null,"Length of pwd is invalid");
return false;*/
}
//Email的格式
int posOfAt,posOfDot;
posOfAt=email.indexOf("@");
posOfDot=email.indexOf(".");
if(posOfAt==-1||posOfDot==-1||(posOfDot-posOfAt)==1||posOfAt>posOfDot||posOfAt==0||posOfDot==email.length()-1)
{
JOptionPane.showMessageDialog(null,"Invalid format of email");
return false;
}
return true;
}
}
import java.awt.*;
import java.awt.event.*;
class MeFrame extends JFrame
{
MeFrame()
{
getContentPane().add(new hiPanel(this));
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
JFrame f=(JFrame)we.getSource();
int result;
result=JOptionPane.showConfirmDialog(null,"现在要退出?","消息",JOptionPane.OK_CANCEL_OPTION);
if(result==0)
{
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else
{
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
}
);
}
public static void main(String a[])
{
new MeFrame();
}
}class NameLengthException extends Exception
{
String message;
NameLengthException(int min,int max)
{
message="名字必须在"+String.valueOf(min)+"和"+String.valueOf(max)+"之间";
}
public String getMessage()
{
return message;
}
}
class PasswordLengthException extends Exception
{
String message;
PasswordLengthException(int min,int max)
{
message="密码必须在"+String.valueOf(min)+"和"+String.valueOf(max)+"之间";
}
public String getMessage()
{
return message;
}
}class CustInfo
{
String name=new String("");
String pwd=new String("");
String gender=new String("");
String phone=new String("");
String email=new String("");
boolean validate() throws NameLengthException,PasswordLengthException
{
if(name.length()>8||name.length()<12)
{
throw new NameLengthException(0,8);
// JOptionPane.showMessageDialog(null,"Name is error");
// return false;
}
if(pwd.length()>8||pwd.length()<12)
{
throw new PasswordLengthException(0,8);
/* JOptionPane.showMessageDialog(null,"Length of pwd is invalid");
return false;*/
}
//Email的格式
int posOfAt,posOfDot;
posOfAt=email.indexOf("@");
posOfDot=email.indexOf(".");
if(posOfAt==-1||posOfDot==-1||(posOfDot-posOfAt)==1||posOfAt>posOfDot||posOfAt==0||posOfDot==email.length()-1)
{
JOptionPane.showMessageDialog(null,"Invalid format of email");
return false;
}
return true;
}
}
{
JRadioButton male,female;
ButtonGroup bg;
genderpanel()
{
male=new JRadioButton("male");
female=new JRadioButton("female");
bg=new ButtonGroup();
male.setSelected(true);
bg.add(male);
bg.add(female);
add(male);
add(female);
}
}
class PhoneField extends JTextField implements KeyListener
{
PhoneField(int length)
{
super(length);
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
int ascii=ke.getKeyChar();
if(ascii>57||ascii<48)
{
ke.consume();
}
}
}
好了代码发完了,希望高手帮我看一看
{
if(name.length()>8||name.length()<12)
{
throw new NameLengthException(0,8);
//JOptionPane.showMessageDialog(null,"Name is error");
//return false;
}
if(pwd.length()>8||pwd.length()<12)
{
throw new PasswordLengthException(0,8);
/*JOptionPane.showMessageDialog(null,"Length of pwd is invalid");
return false;*/
}
主要是这一段有问题了,如果括号里是大于号就没有问题,是小于号就会出现问题,执行以后,就会出问题了,姓名如果输入9位,还是会显示你输入的不对
分析一下:
name.length = 0 ,是 <12的,所以是true,那么name.length 小于0就会返回true(当然不会发生)
name.length = 12 ,是 > 8的,所以是true,那么name.length 大于8的都会返回true代码并不奇怪,应该使用&&而不是||。