?? findaccountname.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class FindAccountName extends JInternalFrame implements ActionListener {
private JPanel jpFind = new JPanel();
private JLabel lbNo, lbName, lbDate, lbBal;
private JTextField txtNo, txtName, txtDate, txtBal;
private JButton btnFind, btnCancel;
private int count = 0;
private int rows = 0;
private int total = 0;
//String Type Array use to Load Records From File.
private String records[][] = new String [500][6];
private FileInputStream fis;
private DataInputStream dis;
FindAccountName () {
// super(Title, Resizable, Closable, Maximizable, Iconifiable)
super ("Search Account Holder", false, true, false, true);
setSize (350, 235);
jpFind.setLayout (null);
lbNo = new JLabel ("Account No:");
lbNo.setForeground (Color.black);
lbNo.setBounds (15, 20, 80, 25);
lbName = new JLabel ("Person Name:");
lbName.setForeground (Color.black);
lbName.setBounds (15, 55, 80, 25);
lbDate = new JLabel ("Last Transaction:");
lbDate.setForeground (Color.black);
lbDate.setBounds (15, 90, 100, 25);
lbBal = new JLabel ("Balance:");
lbBal.setForeground (Color.black);
lbBal.setBounds (15, 125, 80, 25);
txtNo = new JTextField ();
txtNo.setEnabled (false);
txtNo.setHorizontalAlignment (JTextField.RIGHT);
txtNo.setBounds (125, 20, 200, 25);
txtName = new JTextField ();
txtName.setBounds (125, 55, 200, 25);
txtDate = new JTextField ();
txtDate.setEnabled (false);
txtDate.setBounds (125, 90, 200, 25);
txtBal = new JTextField ();
txtBal.setHorizontalAlignment (JTextField.RIGHT);
txtBal.setEnabled (false);
txtBal.setBounds (125, 125, 200, 25);
//Aligning The Buttons.
btnFind = new JButton ("Search");
btnFind.setBounds (20, 165, 120, 25);
btnFind.addActionListener (this);
btnCancel = new JButton ("Cancel");
btnCancel.setBounds (200, 165, 120, 25);
btnCancel.addActionListener (this);
//Adding the All the Controls to Panel.
jpFind.add (lbNo);
jpFind.add (txtNo);
jpFind.add (lbName);
jpFind.add (txtName);
jpFind.add (lbDate);
jpFind.add (txtDate);
jpFind.add (lbBal);
jpFind.add (txtBal);
jpFind.add (btnFind);
jpFind.add (btnCancel);
//Adding Panel to Window.
getContentPane().add (jpFind);
populateArray (); //Load All Existing Records in Memory.
//In the End Showing the New Account Window.
setVisible (true);
}
//Function use By Buttons of Window to Perform Action as User Click Them.
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnFind) {
if (txtName.getText().equals("")) {
JOptionPane.showMessageDialog (this, "Please! Provide Name of Customer to Search.",
"BankSystem - EmptyField", JOptionPane.PLAIN_MESSAGE);
txtName.requestFocus();
}
else {
rows = 0;
populateArray (); //Load All Existing Records in Memory.
findRec (); //Finding if Account No. Exist or Not.
}
}
if (obj == btnCancel) {
txtClear ();
setVisible (false);
dispose();
}
}
//Function use to load all Records from File when Application Execute.
void populateArray () {
try {
fis = new FileInputStream ("Bank.dat");
dis = new DataInputStream (fis);
//Loop to Populate the Array.
while (true) {
for (int i = 0; i < 6; i++) {
records[rows][i] = dis.readUTF ();
}
rows++;
}
}
catch (Exception ex) {
total = rows;
if (total == 0) {
JOptionPane.showMessageDialog (null, "Records File is Empty.\nEnter Records First to Display.",
"BankSystem - EmptyFile", JOptionPane.PLAIN_MESSAGE);
btnEnable ();
}
else {
try {
dis.close();
fis.close();
}
catch (Exception exp) { }
}
}
}
//Function use to Find Record by Matching the Contents of Records Array with ID TextBox.
void findRec () {
boolean found = false;
for (int x = 0; x < total; x++) {
if (records[x][1].equalsIgnoreCase (txtName.getText())) {
found = true;
showRec (x);
break;
}
}
if (found == false) {
JOptionPane.showMessageDialog (this, "Customer Name " + txtName.getText () + " doesn't Exist.",
"BankSystem - WrongNo", JOptionPane.PLAIN_MESSAGE);
txtClear ();
}
}
//Function which display Record from Array onto the Form.
public void showRec (int intRec) {
txtNo.setText (records[intRec][0]);
txtName.setText (records[intRec][1]);
txtDate.setText (records[intRec][2] + ", " + records[intRec][3] + ", " + records[intRec][4]);
txtBal.setText (records[intRec][5]);
}
//Function use to Clear all TextFields of Window.
void txtClear () {
txtNo.setText ("");
txtName.setText ("");
txtDate.setText ("");
txtBal.setText ("");
txtName.requestFocus ();
}
//Function use to Lock Controls of Window.
void btnEnable () {
txtName.setEnabled (false);
btnFind.setEnabled (false);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -