闲来无事,敲着玩玩。
代码:
package ATM; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ATM extends JFrame implements ActionListener { private JPanel pan; private JPanel sec; private JButton log; static JLabel text1=new JLabel("账号"); static JLabel text2=new JLabel("密码"); static JLabel text3=new JLabel("存款"); static JLabel text4=new JLabel("取款"); static JLabel text5=new JLabel("余额"); static JTextField account=new JTextField("",18); static JTextField password=new JTextField("",18); static JTextField savein=new JTextField("",12); static JButton saveb=new JButton("确认"); static JTextField takeout=new JTextField("",12); static JButton takeb=new JButton("确认"); static JTextField left=new JTextField("",12); static int money=10000; String zero=Integer.toString(0); String acc=new String("888888"); String pas=new String("888888"); ATM() { this.setBounds(400, 300, 260, 200); pan = new JPanel(); log = new JButton("登录"); log.addActionListener(this); this.add(pan); this.setAlwaysOnTop(true); this.setVisible(true); pan.add(text1); pan.add(account); pan.add(text2); pan.add(password); pan.add(log); this.setTitle("ATM"); } public static void main(String args[]) { new ATM(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==log) { String ac=account.getText(); String pa=password.getText(); if(ac.equals(acc)&&pa.equals(pas)) { this.setVisible(false); new Another(); } } } class Another extends JFrame { Another() { sec = new JPanel(); this.add(sec); this.setTitle("ATM"); this.setBounds(400, 300, 260, 200); this.setVisible(true); sec.add(text3); sec.add(savein); sec.add(saveb); sec.add(text4); sec.add(takeout); sec.add(takeb); sec.add(text5); sec.add(left); String M=Integer.toString(money); left.setText(M); left.setEditable(false); savein.setText(zero); takeout.setText(zero); saveb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String tmp=savein.getText(); int val=Integer.parseInt(tmp); savein.setText(zero); takeout.setText(zero); if(val>0) { money+=val; String M=Integer.toString(money); left.setText(M); left.setEditable(false); JOptionPane.showMessageDialog(null,"存款成功!"); } else { JOptionPane.showMessageDialog(null,"你逗我呢吧!"); } } }); takeb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String tmp=takeout.getText(); int val=Integer.parseInt(tmp); savein.setText(zero); takeout.setText(zero); if(val>=0 && val<=money && val%100==0) { money-=val; String M=Integer.toString(money); left.setText(M); left.setEditable(false); JOptionPane.showMessageDialog(null,"取款成功!"); } else { JOptionPane.showMessageDialog(null,"非法操作!"); } } }); } } }
原文地址:http://blog.csdn.net/david_jett/article/details/45922509