?? bank.java
字號:
String fileName = new String(); //在對話框中獲得的文件名
JLabel labelPassword = new JLabel("密碼", JLabel.CENTER);
JPasswordField passTF=new JPasswordField(15);
String password=new String();
JLabel serverLabel =new JLabel("賬號",JLabel.CENTER);
JTextField serverTF =new JTextField(15);
String servername=new String();
JButton ok = new JButton("確認(rèn)");
JButton cancel = new JButton("取消");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel ok_CancelPanel = new JPanel();
// 構(gòu)造方法
FileNameDialog(){
super(MoneyFrame.this,"對話框",true);
Container dialogContentPane = getContentPane();
dialogContentPane.setLayout(new GridLayout(4,1)); //將對話框的內(nèi)容板分成上中下3部分
p1.add(fileNameLabel); p1.add(fileNameTF);
fileNameLabel.setFont(new Font("楷體",Font.PLAIN,20));
fileNameTF.setText("");
dialogContentPane.add(p1);
p2.add(labelPassword); p2.add(passTF);
labelPassword.setFont(new Font("楷體",Font.PLAIN,20));
passTF.setText("");
dialogContentPane.add(p2);
p3.add(serverLabel);p3.add(serverTF);
serverLabel.setFont(new Font("楷體",Font.PLAIN,20));
serverTF.setText("");
dialogContentPane.add(p3);
ok_CancelPanel.add(ok);
ok_CancelPanel.add(cancel);
ok.setFont(new Font("楷體",Font.PLAIN,20));
cancel.setFont(new Font("楷體",Font.PLAIN,20));
dialogContentPane.add(ok_CancelPanel);
ok.addActionListener(new ButtonListener());
cancel.addActionListener(new ButtonListener());
setBounds(200,200,350,200);
addWindowListener(new DialogWindowListener());
}//構(gòu)造方法FileNameDialog()聲明結(jié)束
/* 類ButtonListener響應(yīng)對話框類NewFileDialog中的確認(rèn)或取消事件,
將ButtonListener作為對話框類NewFileDialog的內(nèi)部類。 */
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == ok){
//獲取對話框中的文件名
String temp = fileNameTF.getText();
if(!(temp.equals(""))) fileName = temp;
char[]pwod= passTF.getPassword();
password=new String(pwod);
String serve=serverTF.getText();
if(!(serve.equals(""))) servername=serve;
}
fileNameTF.setText("");
passTF.setText("");
serverTF.setText("");
//關(guān)閉對話框
dispose();
}
}
}//文件名對話框類FileNameDialog的聲明結(jié)束
class DialogWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
dispose(); //回收對話框資源
}
}
//監(jiān)聽取款
class NewtakeFieldListener implements ActionListener{
public void actionPerformed(ActionEvent e){
SaveDialog pause=new SaveDialog();
if(sign==0){pause.show();}
accountField[1].setVisible(true);
dialogs.p3.setVisible(true);
dialogs.p2.setVisible(true);
acpanel.setVisible(true);
tablepanel1.setVisible(true);
takepanel.setVisible(true);
tablepanel2.setVisible(true);
prompt.setText("");
for(int f=0;f<takeField.length;f++)takeField[f].setText("");
for(int d=0;d<accountField.length;d++)accountField[d].setText("");
dialogs.show();
tadate=(int)Math.round(System.currentTimeMillis()/86400000);
open();
for(int i=0;i<8;i++)accountField[i].setEditable(false);
for(int f=0;f<takeField.length;f++)takeField[f].setEditable(true);
if((asd.state!=1)&(asd.cancelmoney!=1)){
int fee=Integer.valueOf(asd.acsubject[4]).intValue();
double profit=doProfit(fee,asd.type,asd.dates,tadate);
String s1=Double.toString(profit);
String s2=Double.toString(profit+fee);
asd.tasubject[1]=s1;
asd.tasubject[2]=s2;
if((asd.acsubject[1]).equals(dialogs.password)&
(asd.acsubject[2].equals(dialogs.servername))){
showMoney();
Calendar calendar=Calendar.getInstance();
takeField[0].setText(Integer.toString(calendar.get(Calendar.YEAR))+"年"+
Integer.toString(calendar.get(Calendar.MONTH)+1)+"月"
+Integer.toString(calendar.get(Calendar.DATE))+"日");
prompt.setText("儲種為"+Integer.toString(asd.type)+"年");
asd.state=1;
sign=0;
}
else{ prompt.setText("密碼錯");}
}
else{ showMoney(); for(int i=0;i<4;i++)takeField[i].setEditable(false);
prompt.setText("已取款或已掛失");
}
}}
//顯示文件
void showMoney(){
for(int k=0;k<accountField.length;k++)accountField[k].setText(asd.acsubject[k]);
for(int j=0;j<takeField.length;j++)takeField[j].setText(asd.tasubject[j]);
if(asd.cancelmoney==1){gua=new JCheckBox("是",true);}
}
//保存
void save(){
try{
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(accountField[0].getText(),false)));
for(int h=0;h<accountField.length;h++){ if(h==6)continue;
if(accountField[h].getText().equals(""))throw new IOException();}
asd.write(out);
out.close();
if(asd.type==0){throw new Exception();}
}
catch(IOException ioe){ prompt.setText("無法存入指定文件"); }
catch(Exception e){ prompt.setText("請選擇儲種");}
}
//從指定的文件將數(shù)據(jù)讀入
void open(){
try{
BufferedReader in = new BufferedReader(new FileReader(dialogs.fileName));
asd.read(in);
in.close();
}catch(IOException ioe){prompt.setText("文件沒有找到"); }
}
class OpenListener implements ActionListener{
public void actionPerformed(ActionEvent e){
SaveDialog pause=new SaveDialog();
if(sign==0){pause.show();}
//彈出文件名對話框
dialogs.p2.setVisible(false);
dialogs.p3.setVisible(true);
dialogs.show();
prompt.setText("");
for(int i=0;i<8;i++)accountField[i].setText("");
for(int f=0;f<takeField.length;f++)takeField[f].setText("");
open();
if(asd.acsubject[2].equals(dialogs.servername)){
showMoney();
if((asd.state==1)|(asd.cancelmoney==1)){
prompt.setText("已取款或已掛失");
} else{
prompt.setText("儲種為"+Integer.toString(asd.type)+"年");
sign=0;
}
for(int i=0;i<8;i++){
accountField[i].setEditable(false);}
for(int f=0;f<takeField.length;f++)takeField[f].setEditable(false);
accountField[1].setVisible(false);
accountField[6].setEditable(true);
}
else{prompt.setText("輸入有誤");}
takepanel.setVisible(true);
tablepanel2.setVisible(true);
}
}
class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
prompt.setText("");
save() ;
asd.state=0;
sign=1;
}}
class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
SaveDialog pause=new SaveDialog();
if(sign==0){pause.show();}
System.exit(0); }
}
//計(jì)算利息
double doProfit(int stock,int style,int dates1,int dates2){
double profits=0;
if(style*365==(dates2-dates1)){
switch(style){
case 1: profits= (double)(Math.round(stock*1.95/100)); break;
case 3: profits= (double)(Math.round(stock*2.25/100*3));break;
case 5: profits= (double)(Math.round(stock*2.52/100*5));break;
} }
else { profits=(double)(Math.round(stock*(dates2-dates1)*0.005/100));}
return profits;
}
class OneUser{
String[]acsubject=new String[8];
String[]tasubject=new String[4];
int cancelmoney=0; //"0"表示沒有掛失 " 1"表示掛失
int type; // 1,3,5 year
int dates;
int state=0;
void write(BufferedWriter out) throws IOException{
out.write(Integer.toString(state)); out.newLine();
out.write(Integer.toString(cancelmoney)); out.newLine();
out.write(Integer.toString(dates)); out.newLine();
out.write(Integer.toString(type));out.newLine();
for(int k=0;k<8;k++)acsubject[k]=accountField[k].getText();
for(int b=0; b<8; b++)
{out.write(acsubject[b]);out.newLine(); }
if(!(takeField[1].getText().equals(""))){
for(int f=0;f<4;f++){tasubject[f]=takeField[f].getText();
out.write(tasubject[f]); out.newLine(); }
}
}
void read(BufferedReader in) throws IOException{
state=Integer.valueOf(in.readLine()).intValue();
cancelmoney=Integer.valueOf(in.readLine()).intValue();
dates=Integer.valueOf(in.readLine()).intValue();
type=Integer.valueOf(in.readLine()).intValue();
for(int i=0; i<8; i++) acsubject[i]=in.readLine();
String str;
if (( str=in.readLine())!=null){
tasubject[0]=str;
for(int j=1;j<4;j++)tasubject[j]=in.readLine();
}
}
} //類OneUser聲明結(jié)束
}//類MoneyFrame的聲明結(jié)束
public class Bank{
public static void main(String[]args){
MoneyFrame win = new MoneyFrame( );
win.setVisible(true);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -