📄 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("确认");
JButton cancel = new JButton("取消");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel ok_CancelPanel = new JPanel();
// 构造方法
FileNameDialog(){
super(MoneyFrame.this,"对话框",true);
Container dialogContentPane = getContentPane();
dialogContentPane.setLayout(new GridLayout(4,1)); //将对话框的内容板分成上中下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());
}//构造方法FileNameDialog()声明结束
/* 类ButtonListener响应对话框类NewFileDialog中的确认或取消事件,
将ButtonListener作为对话框类NewFileDialog的内部类。 */
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("");
//关闭对话框
dispose();
}
}
}//文件名对话框类FileNameDialog的声明结束
class DialogWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
dispose(); //回收对话框资源
}
}
//监听取款
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("请选择储种");}
}
//从指定的文件将数据读入
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); }
}
//计算利息
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声明结束
}//类MoneyFrame的声明结束
public class Bank{
public static void main(String[]args){
MoneyFrame win = new MoneyFrame( );
win.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -