0

1.


it is a program that can read serial number.if it dosn't match the number pre-defined,it will gives an error message, else it will give a right message.however,it always gives error messages,No matter how i tried,even when it matches the number pre-defined,it gives error. the program compiles normally,just running beyond my expection.

2. Judge.java

public class Judge {
    public static void main(String args[])
    {
        Win win=new Win();
        win.setTitle("judge serial number");
        win.setBounds(10,10,460,360);
    }
}

Win.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*contains:
 * 1.three areafield
 * 2.a botton
 * 3.keylistener,focuslistener
 */
public class Win extends JFrame implements ActionListener//Win类用来初始化窗口
{
    JTextField text[]=new JTextField[3]; //三个文本框
    String str[]=new String[5];//字符串数组
    focusPolice focuspolice;    //自定义类2:焦点事件类。
    JButton b;      //确定按钮
                    //构造函数   
    Win() {
          setLayout(new FlowLayout());      //布局类型为FlowLayout型
          b=new JButton("确定");
          this.init();
          add(b);
          text[0].requestFocusInWindow();       //初始化焦点
          setVisible(true); 
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    public void init()
    {
          focuspolice=new focusPolice();    //创建焦点事件类对象
          for(int i=0;i<3;i++)
          {
              text[i]=new JTextField(4);
              text[i].addFocusListener(focuspolice);
              text[i].addKeyListener(focuspolice);
              str[i]=text[i].getText().toString();
              add(text[i]);
          }  
          b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
            {
                if(ae.getActionCommand().equals("确定"))
                {
                if(str[0]=="aaa"&&str[1]=="bbb"&&(str[2]=="ccc"))
                {
                    JOptionPane.showMessageDialog(b,"正确!","提示框",
                          JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(b,"错误!","提示框",
                          JOptionPane.WARNING_MESSAGE);
                }
                }
            }

    }

focusPolice.java

import java.awt.event.*;
import javax.swing.*;       //既然是要处理事件,那么awt.event必不可少
public class focusPolice implements KeyListener,FocusListener{//不光是需要FocusListener,也需要keyListener
       public void keyPressed(KeyEvent e) {
              JTextField t=(JTextField)e.getSource();
              if(t.getCaretPosition()>=2)           
                  //实验结果:如果输入的字符个数多于3个,将会自动跳焦。
                 t.transferFocus(); 
            }
            public void keyTyped(KeyEvent e) {}
            public void keyReleased(KeyEvent e) {}
            public void focusGained(FocusEvent e) {
              JTextField text=(JTextField)e.getSource();
              text.setText(null); 
            }
            public void focusLost(FocusEvent e){}

}

enter image description here

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
Victor
  • 23
  • 4

1 Answers1

0

First Problem : Using == to compare strings.

Change

if(str[0]=="aaa"&&str[1]=="bbb"&&(str[2]=="ccc"))

to

if(str[0].equals("aaa")&&str[1].equals("bbb")&&str[2].equals("ccc"))

Second Problem : You never change the values of str[i] to those of text[i] except in init where it is pointless since the textfields are empty then.
During your if all str[i] are empty strings.
Either change their values when you type into the textfields or get the strings directly from the textfields which seems to be the easier way.

if(text[0].getText().toString().equals("aaa")&&text[1].getText().toString().equals("bbb")&&text[2].getText().toString().equals("ccc"))
Turamarth
  • 2,282
  • 4
  • 25
  • 31