⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10.22(jradiobutton有点问题版).txt

📁 模拟atm程序 java语言编写 可以进行密码修改
💻 TXT
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;

class MyAtm extends JFrame implements ActionListener
{
//	String user = "123456";
//	String pass = "123456";
//	double balance = 10000;
	UserInfo userInfo[] = new UserInfo[2];
	int index;//登陆时账号的下标
	int index1;//转账时被转入账号的下标
	
	Icon pic1;
	Icon pic2;
	
	
	boolean loginStatus = false;//防止在未登陆的情况下进行操作
	boolean flag1 = false;//防止在未按Alter Password的情况下输如新密码
	boolean flag2 = false;//防止在未输入旧密码的情况下修改密码
	boolean flag3 = false;//控制在按"Withdrawal"按键之后的文本信息和标签的显示
	boolean flag4 = false;//控制在按"Deposit"按键之后的文本信息和标签的显示
	boolean flag5 = false;//控制在按"Transfer"按键之后的文件信息和标签的显示
	boolean flagAccount = false;//控制数字按键是输入到用户名框
	boolean flagPassword = false;//控制数字按键是输入到密码框
	boolean flagTrans = true;//控制转账时先输入转入的账号
//	boolean flagMoney = false;//控制在输入转入账号后,转账金额的输入
	
	JTabbedPane tabbedPane;
	JPanel flowLayoutPanel;
	JPanel borderLayoutPanel;
	JPanel alterPanel;
	JPanel businessPanel;
	
	Box boxLayoutPanel;
	FlowLayout flow; 
	BorderLayout border;
	FlowLayout flow1;
	
	JButton account; 
	JButton password;
	JLabel new1Label;
	JLabel oldLabel;
	JLabel new2Label;
	JLabel businessLabel;
	
	JTextField receiveInput;
	JTextField prompt;//修改密码时的提示框
	JPasswordField passwordField;
	JPasswordField oldField;
	JPasswordField new1Field;
	JPasswordField new2Field;
	JTextField businessField;
	
	JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, 
			btn9, btn0, btn00, btn, btnStar;
	JButton exit1;
	JButton enterButton;
	JButton cancelButton;
	JButton withdrawal;
	JButton deposit;
	JButton transfer;
	JButton exit;
	JButton inquiryButton;
	JButton exit2;
	JButton alter;
	JButton exit3;
	
	JRadioButton oneHundred, twoHundred, fiveHundred, thousand, twoThousand, otherAmount;
	ButtonGroup radioGroup;
	
	JTextArea text;
//	JTextArea businessArea;
	JTextArea inquiryText;
	Font font;
	
	Container con;

	
	
	public MyAtm(String s)
	{
		super(s);
		
		for(int i = 0; i < 2; i++)
		userInfo[i]= new UserInfo();
 		userInfo[0].setUserNum("123456");
		userInfo[0].setPassword("123456");
		userInfo[0].setBalance(10000);
		
		userInfo[1].setUserNum("654321");
		userInfo[1].setPassword("654321");
		userInfo[1].setBalance(10000);
	//	userInfo = new UserInfo();
 	//	userInfo.setUserNum("123456");
	//	userInfo.setPassword("123456");
	//	userInfo.setBalance(10000);
		
		pic1 = new ImageIcon("01.jpg");
		pic2 = new ImageIcon("02.jpg");
		
		account = new JButton("Account");
		account.setBackground(Color.pink);
		account.addActionListener(this);
		password = new JButton("Password");
		password.addActionListener(this);
		password.setBackground(Color.pink);
		receiveInput = new JTextField(11);
		receiveInput.addActionListener(this);
	//	receiveInput.setBackground(Color.pink);
		passwordField = new JPasswordField(10);
		passwordField.addActionListener(this);
	//	passwordField.setBackground(Color.pink);
	//	JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btn00, btn, btnStar;
		 //setBackground(Color.blue);//??	
		btn1=new JButton("1");
	//	btn1.setBackground(Color.pink);
		btn1.addActionListener(this);
		
		btn2=new JButton("2");
	//	btn2.setBackground(Color.pink);
		btn2.addActionListener(this);
		
		btn3=new JButton("3");
	//	btn3.setBackground(Color.pink);
		btn3.addActionListener(this);
		
		btn4=new JButton("4");
	//	btn4.setBackground(Color.pink);
		btn4.addActionListener(this);
		
		btn5=new JButton("5");
	//	btn5.setBackground(Color.pink);
		btn5.addActionListener(this);
		
		btn6=new JButton("6");
	//	btn6.setBackground(Color.pink);
		btn6.addActionListener(this);
		
		btn7=new JButton("7");
	//	btn7.setBackground(Color.pink);
		btn7.addActionListener(this);
		
		btn8=new JButton("8");
	//	btn8.setBackground(Color.pink);
		btn8.addActionListener(this);
		
		btn9=new JButton("9");
	//	btn9.setBackground(Color.pink);
		btn9.addActionListener(this);
		
		btn0=new JButton("0");
	//	btn0.setBackground(Color.pink);
		btn0.addActionListener(this);
		
		btn00=new JButton("00");
	//	btn00.setBackground(Color.pink);
		btn00.addActionListener(this);
		
		btn = new JButton(".");
	//	btn.setBackground(Color.pink);
		btn.addActionListener(this);
		
		btnStar = new JButton("*"); 
	//	btnStar.setBackground(Color.pink);
		btnStar.addActionListener(this);
		
		JButton exit1 = new JButton("Exit");
	//	exit1.setBackground(Color.pink);
		exit1.addActionListener(this);
		
		enterButton = new JButton("Sure");
	//	enterButton.setBackground(Color.pink);
		enterButton.addActionListener(this);
		
		cancelButton = new JButton("Cancel");
	//	cancelButton.setBackground(Color.pink);
		cancelButton.addActionListener(this);
		
		text= new JTextArea("   \n\n Welcome to\n     ** Bank \n     ATM \n (Login first!)");
		Font font=new Font("Arial",Font.BOLD,30);
		text.setFont(font);
		text.setEditable(false);
		text.setBackground(Color.pink);
		
		setBounds(100, 100, 360 ,300);
		setVisible(true);
		 
		tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		
		flow = new FlowLayout();//flow
		flowLayoutPanel = new JPanel();
		flow.setAlignment(FlowLayout.LEFT);//flow
		flow.setVgap(6);//flow????4
		flow.setHgap(10);//????5
		
		flowLayoutPanel.setLayout(flow);
		flowLayoutPanel.add(account);
		flowLayoutPanel.add(receiveInput);
		flowLayoutPanel.add(password);
		flowLayoutPanel.add(passwordField);
		flowLayoutPanel.add(btn1);
		flowLayoutPanel.add(btn2);
		flowLayoutPanel.add(btn3);
		flowLayoutPanel.add(btn4);
		flowLayoutPanel.add(btn5);
		flowLayoutPanel.add(btn6);
		flowLayoutPanel.add(btn7);
		flowLayoutPanel.add(btn8);
		flowLayoutPanel.add(btn9);
		flowLayoutPanel.add(btn0);
		flowLayoutPanel.add(btn00);
		flowLayoutPanel.add(btn);
		flowLayoutPanel.add(btnStar);
		flowLayoutPanel.add(enterButton);
	    flowLayoutPanel.add(cancelButton);
		flowLayoutPanel.add(exit1);
		flowLayoutPanel.setBackground(Color.pink);
		
		businessPanel = new JPanel();
	//	con.add(businessPanel, BorderLayout.CENTER);
		RadioButtonHandler handler = new RadioButtonHandler();
		
		oneHundred = new JRadioButton("100", false);
		businessPanel.add(oneHundred);
		oneHundred.addItemListener(handler);
		twoHundred = new JRadioButton("200", false);
		businessPanel.add(twoHundred);
		twoHundred.addItemListener(handler);
		fiveHundred = new JRadioButton("500",false);
		businessPanel.add(fiveHundred);
		fiveHundred.addItemListener(handler);
		thousand = new JRadioButton("1000", false);
		businessPanel.add(thousand);
		thousand.addItemListener(handler);
		twoThousand = new JRadioButton("2000", false);
		businessPanel.add(twoThousand);
		twoThousand.addItemListener(handler);
		otherAmount = new JRadioButton("Other",false);
		businessPanel.add(otherAmount);
		otherAmount.addItemListener(handler);
		
				
		radioGroup = new ButtonGroup();
		radioGroup.add(oneHundred);
		radioGroup.add(twoHundred);
		radioGroup.add(fiveHundred);
		radioGroup.add(thousand);
		radioGroup.add(twoThousand);
		radioGroup.add(otherAmount);
	//	radioGroup.setVisible(false);
		
		withdrawal = new JButton("Withdrawal");
		withdrawal.addActionListener(this);
		deposit = new    JButton("   Deposit  ");
		deposit.addActionListener(this);
		transfer = new   JButton("   Transfer ");
		transfer.addActionListener(this);
		exit = new       JButton("Exit");
		exit.addActionListener(this);
	//	businessArea = new JTextArea(10,10);
		businessField = new JTextField(10);
		businessField.setVisible(false);
		businessField.addActionListener(this);
		businessLabel = new JLabel();
		businessLabel.setVisible(false);
	//	businessArea.add(businessField);
		
		businessPanel.add(businessLabel);
		businessPanel.add(businessField);
	
		boxLayoutPanel = Box.createVerticalBox();
		boxLayoutPanel.add(withdrawal);
		boxLayoutPanel.add(deposit);
		boxLayoutPanel.add(transfer);
	//	boxLayoutPanel.add(businessLabel);
	//	boxLayoutPanel.add(businessField);
		boxLayoutPanel.add(businessPanel);
		boxLayoutPanel.add(exit);
		//boxLayoutPanel.setBackground(Color.pink);
		
		inquiryButton = new JButton("Balance Inquiry");
		inquiryButton.addActionListener(this);
		exit2 = new JButton("Exit");
		exit2.addActionListener(this);
		inquiryText = new JTextArea();
		inquiryText.setVisible(false);
		
		borderLayoutPanel = new JPanel();
		border = new BorderLayout();
		border.setVgap(6);
		border.setHgap(10);
		borderLayoutPanel.setLayout(border);
		borderLayoutPanel.add(inquiryButton, BorderLayout.NORTH);
		borderLayoutPanel.add(inquiryText, BorderLayout.CENTER);
		borderLayoutPanel.add(exit2, BorderLayout.SOUTH);
 	//	borderLayoutPanel.setBackground(Color.pink);
		
		alter = new JButton("Alter Password");
		alter.addActionListener(this);
		exit3 = new JButton("Exit");
		exit3.addActionListener(this);
		oldLabel = new JLabel("Input Old Password and press enter");
		new1Label = new JLabel("Input the New Password here");
		new2Label = new JLabel("Input New Password again & press enter");
		oldField = new JPasswordField(10);	
		oldField.addActionListener(this);
		new1Field = new JPasswordField(10);
	//	new1Field.addActionListener(this);
		new2Field = new JPasswordField(10);
		new2Field.addActionListener(this);
		
		alterPanel = new JPanel();
		flow1 = new FlowLayout();
		alterPanel.setLayout(flow1);
		alterPanel.add(alter);alterPanel.add(exit3);
		alterPanel.add(oldLabel);
		alterPanel.add(oldField);
		alterPanel.add(new1Label);
		alterPanel.add(new1Field);
		alterPanel.add(new2Label);
		alterPanel.add(new2Field);
		alterPanel.setBackground(Color.pink);
		
		businessField.setFont(font);
		
		tabbedPane.add("StartPage",text);
		tabbedPane.add("Login",flowLayoutPanel);
		tabbedPane.add("Business",boxLayoutPanel);
		tabbedPane.add("Inquiry", borderLayoutPanel);
		tabbedPane.add("Alter",alterPanel);
		tabbedPane.validate();
		
		Container con = getContentPane();
		con.setBackground(Color.blue);
		con.add(tabbedPane);
		con.validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		prompt = new JTextField(15);//修改密码的时候的提示两次输入是否一致
		prompt.setBackground(Color.pink);
		alterPanel.add(prompt);
	}
	
	
	class RadioButtonHandler implements ItemListener
	{
		public void itemStateChanged(ItemEvent e)
		{
			double amount;
			
			if(e.getSource() == oneHundred)
			{
				amount = 100;
				if(flag3 == true)
				{
					if(amount > userInfo[index].balance)
					JOptionPane.showMessageDialog(null, "No enough money in your account\n     Select again Or exit");
					else
					{
					userInfo[index].balance = userInfo[index].balance - amount;
					JOptionPane.showMessageDialog(null, "Drawal Success " + Double.toString(amount));
					businessLabel.setVisible(false);
					businessField.setVisible(false);
					}
				
				}
				if(flag4 == true)
				{
					userInfo[index].balance = userInfo[index].balance + amount;
				
					JOptionPane.showMessageDialog(null, "Deposit Success " + Double.toString(amount));
					businessLabel.setVisible(false);
					businessField.setVisible(false);
				}
				if(flag5 == true)
				{
					if(flagTrans == false)
					{
				
						if(amount > userInfo[index].balance)
						JOptionPane.showMessageDialog(null, "No enough money in your account");
						else
						{
						userInfo[index].balance = userInfo[index].balance - amount;
						userInfo[index1].balance = userInfo[index1].balance + amount;
						JOptionPane.showMessageDialog(null, "Transfer Success" + Double.toString(amount));
						businessLabel.setVisible(false);
						businessField.setVisible(false);
						}
					}
					if(flagTrans == true)
					{
						for(int i = 0; i < userInfo.length; i++)
						{
					
							if(businessField.getText().equals(userInfo[i].userNum) && businessField.getText().equals(userInfo[index].userNum))
							{
							JOptionPane.showMessageDialog(null, "Can't transfer to self");
							businessField.setText("");
							break;
							}
						
							if(businessField.getText().equals(userInfo[i].userNum) && !businessField.getText().equals(userInfo[index].userNum))
							{
							businessLabel.setText("How much to transfer and press enter");
							businessField.setText("");
							index1 = i;
							flagTrans = false;
							break;
							}
						
							if(i == userInfo.length - 1)
							{
							JOptionPane.showMessageDialog(null, "No such User");
							businessField.setText("");
							}
																
						}
					}
			 	}
			}
			
			
			else if(e.getSource() == twoHundred)
			{
				amount = 200;
				if(flag3 == true)
				{
					if(amount > userInfo[index].balance)
					JOptionPane.showMessageDialog(null, "No enough money in your account\n     Select again Or exit");
					else
					{
					userInfo[index].balance = userInfo[index].balance - amount;
					JOptionPane.showMessageDialog(null, "Drawal Success " + Double.toString(amount));
					businessLabel.setVisible(false);
					businessField.setVisible(false);
					}
				
				}
				if(flag4 == true)
				{
					userInfo[index].balance = userInfo[index].balance + amount;
				
					JOptionPane.showMessageDialog(null, "Deposit Success " + Double.toString(amount));
					businessLabel.setVisible(false);
					businessField.setVisible(false);
				}
				if(flag5 == true)
				{
					if(flagTrans == false)
					{
				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -