- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;//主窗口部分class Window extends JFrame implements ActionListener{ JTextField cardnumber; /*卡号*/ JPasswordField password; /*密码*/ JButton b1,b2; public Window(){ super("欢迎进入银行系统"); Container cont=getContentPane(); cont.setLayout(new FlowLayout()); cont.add(new JLabel("卡号")); cardnumber=new JTextField(10); cont.add(cardnumber); cont.add(new JLabel("密碼")); password=new JPasswordField(10); password.setEchoChar('*'); cont.add(password); b1=new JButton("确定"); b2=new JButton("退卡"); cont.add(b1); b1.addActionListener(this); cont.add(b2); b2.addActionListener(this); this.addWindowListener(new WindowAdapter(){ //完全关闭窗口 public void windowClosing(WindowEvent e){ System.exit(0); } }); setBounds(120,125,200,175); setVisible(true); }// public window public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("确定")){ if(cardnumber.getText().equals(123456)&&password.equals(123456)) JOptionPane.showMessageDialog(null, "yes"); else JOptionPane.showMessageDialog(null, "信息输入错误"); } //if if(e.getActionCommand().equals("退卡")) System.exit(0); //退卡 }//actionPerformed }//windowpublic class ATM{ public static void main(String args[]){ new Window(); }}
------解决方案--------------------
getText()获取的值是String类型的,cardnumber.getText().equals(123456)肯定为false了。
------解决方案--------------------
String number1, number2;// 输入的两个卡号和密码的变量
number1 = cardnumber.getText();
number2 = password.getText();
if (number1.equals("123456") && number2.equals("123456"))