我有一段这样的代码:import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
class qq_frame3_1 extends JFrame implements ActionListener
{
JLabel lab_name=new JLabel("用户昵称:");
JLabel lab_password=new JLabel("用户密码:");
JLabel lab_repassword=new JLabel("确认密码:");
JLabel lab_kidney=new JLabel("个性签名:");
JLabel lab_truename=new JLabel("真实姓名:");
JLabel lab_sex=new JLabel("性别:");
JLabel lab_age=new JLabel("年龄:");
JLabel lab_webpage=new JLabel("个人主页:");
JLabel lab_note=new JLabel("个人说明:");
JTextField tf_name=new JTextField();
JTextField tf_truename=new JTextField();
JTextField tf_age=new JTextField();
JTextField tf_webpage=new JTextField();
JPasswordField pf_password=new JPasswordField();
JPasswordField pf_repassword=new JPasswordField();
JTextArea ta_kidney=new JTextArea();
JTextArea ta_note=new JTextArea();
JPanel pane_border=new JPanel(null); 
JButton but_apply=new JButton("申请");
JButton but_cancel=new JButton("取消");
JComboBox comb_ico=new JComboBox();
JComboBox comb_sex=new JComboBox();
Border border=BorderFactory.createLineBorder(Color.blue,1);
ImageIcon imagc1,imagc2,imagc3,imagc4;
qq_frame3_1()
{
Container c=this.getContentPane();
c.setLayout(null);
Image ico=Toolkit.getDefaultToolkit().getImage("84.gif");
imagc1=new ImageIcon("lovergril_2.jpg");
imagc2=new ImageIcon("hyl_2.jpg");
imagc3=new ImageIcon("plmm_2.jpg");
imagc4=new ImageIcon("g_2.jpg");
comb_ico.setBounds(20,20,70,50);
comb_ico.addItem(imagc1);
comb_ico.addItem(imagc2);
comb_ico.addItem(imagc3);
comb_ico.addItem(imagc4);
c.add(comb_ico);
lab_name.setBounds(115,8,80,50);
c.add(lab_name);
tf_name.setBounds(190,19,200,25);
c.add(tf_name);
lab_password.setBounds(115,45,80,50);
c.add(lab_password);
pf_password.setBounds(190,55,100,25);
c.add(pf_password);
lab_repassword.setBounds(300,45,80,50);
c.add(lab_repassword);
pf_repassword.setBounds(375,55,100,25);
c.add(pf_repassword);
lab_kidney.setBounds(20,100,80,30);
c.add(lab_kidney);
ta_kidney.setBounds(115,90,360,70);
c.add(ta_kidney);
pane_border.setBorder(border);
pane_border.setBounds(20,170,455,165);
c.add(pane_border);
lab_truename.setBounds(10,15,70,20);
pane_border.add(lab_truename);
tf_truename.setBounds(90,15,100,20);
pane_border.add(tf_truename);
lab_sex.setBounds(210,15,50,20);
pane_border.add(lab_sex);
comb_sex.setBounds(260,15,50,20);
comb_sex.addItem("男");
comb_sex.addItem("女");
pane_border.add(comb_sex);
lab_age.setBounds(330,15,80,25);
pane_border.add(lab_age);
tf_age.setBounds(370,15,50,20);
pane_border.add(tf_age);
lab_webpage.setBounds(10,45,80,20);
pane_border.add(lab_webpage);
tf_webpage.setBounds(90,45,340,20);
pane_border.add(tf_webpage);
lab_note.setBounds(10,70,80,20);
pane_border.add(lab_note);
ta_note.setBounds(90,75,340,80);
pane_border.add(ta_note);
but_apply.setBounds(395,345,80,20);
c.add(but_apply);
but_cancel.setBounds(290,345,80,20);
c.add(but_cancel);
but_cancel.addActionListener(this);
but_apply.addActionListener(this);
this.setIconImage(ico);
this.setTitle("填写注册信息");
this.setBounds(280,180,500,400);
this.setVisible(true);
}
public int verify()
{
int x=0;
String spw=pf_password.getText();
String srpw=pf_repassword.getText();
String s=tf_webpage.getText().substring(0,6);
String ss="http://";
if(spw.length()<8)
{
JOptionPane.showMessageDialog(null,"密码长度不能小于8位!/n请从新输入");
}
else if(!(spw.equals(srpw)))
{
JOptionPane.showMessageDialog(null,"校验码不正确");
}
     else if(tf_webpage.getText().length()<7)
{
JOptionPane.showMessageDialog(null,"个人主页非法地址/n从新填写");
}
else if(s.equals(ss)==false)
{
JOptionPane.showMessageDialog(null,"个人主页非法地址/n从新填写");
}
else{
x=1;
}

return x;
}
public static void main(String args[])
{
qq_frame3_1 f3=new qq_frame3_1();
f3.setDefaultCloseOperation(3);a
}
public void actionPerformed(ActionEvent e)
{
Object ob=e.getSource();
if(ob==but_cancel)
{
System.exit(1);
}
if(ob==but_apply)
{

if(this.verify()!=0);
JOptionPane.showMessageDialog(null,"注册成功!");
}
}
} 调用verify()判断if—else嵌套时输入错误时怎么总是返回x=1。使得提示成功的对话框总是显示!
而且他们是按if-else书写的反向顺序判断的。
也就是说先判断网页地址在判断密码长度。真是怪怪怪!!

解决方案 »

  1.   

    你自己去加异常...我是按你的意思改的...你IF判断那多了个;号,所以不管怎么样,总是会运行:注册成功;
    这是我改后的代码,解决了你的问题,你程序里面还有很多问题,你慢慢检查吧
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    class qq_frame3_1 extends JFrame implements ActionListener
    {
    String spw=null;
    String srpw=null;
    String s=null;              
    JLabel lab_name=new JLabel("用户昵称:");
    JLabel lab_password=new JLabel("用户密码:");
    JLabel lab_repassword=new JLabel("确认密码:");
    JLabel lab_kidney=new JLabel("个性签名:");
    JLabel lab_truename=new JLabel("真实姓名:");
    JLabel lab_sex=new JLabel("性别:");
    JLabel lab_age=new JLabel("年龄:");
    JLabel lab_webpage=new JLabel("个人主页:");
    JLabel lab_note=new JLabel("个人说明:");
    JTextField tf_name=new JTextField();
    JTextField tf_truename=new JTextField();
    JTextField tf_age=new JTextField();
    JTextField tf_webpage=new JTextField();
    JPasswordField pf_password=new JPasswordField();
    JPasswordField pf_repassword=new JPasswordField();
    JTextArea ta_kidney=new JTextArea();
    JTextArea ta_note=new JTextArea();
    JPanel pane_border=new JPanel(null); 
    JButton but_apply=new JButton("申请");
    JButton but_cancel=new JButton("取消");
    JComboBox comb_ico=new JComboBox();
    JComboBox comb_sex=new JComboBox();
    Border border=BorderFactory.createLineBorder(Color.blue,1);
    ImageIcon imagc1,imagc2,imagc3,imagc4;
    qq_frame3_1()
    {
    Container c=this.getContentPane();
    c.setLayout(null);
    Image ico=Toolkit.getDefaultToolkit().getImage("84.gif");
    imagc1=new ImageIcon("lovergril_2.jpg");
    imagc2=new ImageIcon("hyl_2.jpg");
    imagc3=new ImageIcon("plmm_2.jpg");
    imagc4=new ImageIcon("g_2.jpg");
    comb_ico.setBounds(20,20,70,50);
    comb_ico.addItem(imagc1);
    comb_ico.addItem(imagc2);
    comb_ico.addItem(imagc3);
    comb_ico.addItem(imagc4);
    c.add(comb_ico);
    lab_name.setBounds(115,8,80,50);
    c.add(lab_name);
    tf_name.setBounds(190,19,200,25);
    c.add(tf_name);
    lab_password.setBounds(115,45,80,50);
    c.add(lab_password);
    pf_password.setBounds(190,55,100,25);
    c.add(pf_password);
    lab_repassword.setBounds(300,45,80,50);
    c.add(lab_repassword);
    pf_repassword.setBounds(375,55,100,25);
    c.add(pf_repassword);
    lab_kidney.setBounds(20,100,80,30);
    c.add(lab_kidney);
    ta_kidney.setBounds(115,90,360,70);
    c.add(ta_kidney);
    pane_border.setBorder(border);
    pane_border.setBounds(20,170,455,165);
    c.add(pane_border);
    lab_truename.setBounds(10,15,70,20);
    pane_border.add(lab_truename);
    tf_truename.setBounds(90,15,100,20);
    pane_border.add(tf_truename);
    lab_sex.setBounds(210,15,50,20);
    pane_border.add(lab_sex);
    comb_sex.setBounds(260,15,50,20);
    comb_sex.addItem("男");
    comb_sex.addItem("女");
    pane_border.add(comb_sex);
    lab_age.setBounds(330,15,80,25);
    pane_border.add(lab_age);
    tf_age.setBounds(370,15,50,20);
    pane_border.add(tf_age);
    lab_webpage.setBounds(10,45,80,20);
    pane_border.add(lab_webpage);
    tf_webpage.setBounds(90,45,340,20);
    pane_border.add(tf_webpage);
    lab_note.setBounds(10,70,80,20);
    pane_border.add(lab_note);
    ta_note.setBounds(90,75,340,80);
    pane_border.add(ta_note);
    but_apply.setBounds(395,345,80,20);
    c.add(but_apply);
    but_cancel.setBounds(290,345,80,20);
    c.add(but_cancel);
    but_cancel.addActionListener(this);
    but_apply.addActionListener(this);
    this.setIconImage(ico);
    this.setTitle("填写注册信息");
    this.setBounds(280,180,500,400);
    this.setVisible(true);
    }
    public int verify()
    {
    int x=0;
    spw=pf_password.getText();
    srpw=pf_repassword.getText();
    s=tf_webpage.getText().substring(0,7);
    String ss="http://";
    if(spw.length()<8)
    {
    JOptionPane.showMessageDialog(null,"密码长度不能小于8位!/n请从新输入");
    }
    else if(!(spw.equals(srpw)))
    {
    JOptionPane.showMessageDialog(null,"校验码不正确");
    }
         else if(tf_webpage.getText().length()<7)
    {
    JOptionPane.showMessageDialog(null,"个人主页非法地址/n从新填写");
    return 0;
    }
    else if(s.equals(ss)==false)
    {
    JOptionPane.showMessageDialog(null,"个人主页非法地址/n从新填写");
    return 0;
    }
    else{
    x=1;
    }

    return x;
    }
    public static void main(String args[])
    {
    qq_frame3_1 f3=new qq_frame3_1();
    f3.setDefaultCloseOperation(3);
    }
    public void actionPerformed(ActionEvent e)
    {
    Object ob=e.getSource();
    if(ob==but_cancel)
    {
    System.exit(1);
    }
    if(ob==but_apply)
    {

    if(this.verify()!=0)
    JOptionPane.showMessageDialog(null,"注册成功!");
    }
    }
    }
      

  2.   

    用IDE检查下拼写就可以解决的问题,认真就OK
      

  3.   

    还有substring(int n,int j);取的值是从n到j-1的值,所以应改成s=tf_webpage.getText().substring(0,7);
      如果满意请给分