import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
//主窗口
public class Window extends JFrame {
static String username;// 用于存取用户名
Button search, draw, deposit, modi, comeback, submit1, cancel1, submit2,
cancel2, submit3, cancel3;
Panel left, right, cards;
DBase db;
TextArea text1, text2, text3;
TextField money1, money2;
JPasswordField password1, password2, password3;
static String[] texts = { "welcomePanel", "searchPanel", "drawPanel",
"depositPanel", "modiPanel" };
double balance;
// 构建欢迎面板
private Panel createWelcomePanel() {
Panel panel = new Panel();
panel.setLayout(null);
panel.setSize(300, 300);
Panel welcome = new Panel();
welcome.setLayout(new GridLayout(2, 1));
welcome.setBounds(40, 90, 200, 100);
Label label = new Label("银行模拟系统", Label.CENTER);
label.setForeground(Color.RED);
label.setFont(new Font("宋体", Font.BOLD, 20));
welcome.add(label);
panel.add(welcome);
return panel;
}
// 构建查询面板
private Panel createSearchPanel() {
Panel panel = new Panel();
panel.setLayout(null);
panel.setSize(300, 300);
Panel searchPanel = new Panel();
searchPanel.setBounds(0, 40, 260, 200);
searchPanel.setLayout(new GridLayout(3, 2));
Label cardID1 = new Label("账户:", Label.RIGHT);
cardID1.setFont(new Font("宋体", Font.BOLD, 15));
Label cardID2 = new Label(username);
cardID2.setFont(new Font("宋体", Font.BOLD, 15));
String name = db.getName(username);
Label name1 = new Label("名字:", Label.RIGHT);
name1.setFont(new Font("宋体", Font.BOLD, 15));
Label name2 = new Label(name);
name2.setFont(new Font("宋体", Font.BOLD, 15));
balance = db.getBalance(username);
Label balance1 = new Label("余额:", Label.RIGHT);
balance1.setFont(new Font("宋体", Font.BOLD, 15));
Label balance2 = new Label("" + balance);
balance2.setFont(new Font("宋体", Font.BOLD, 15));
searchPanel.add(cardID1);
searchPanel.add(cardID2);
searchPanel.add(name1);
searchPanel.add(name2);
searchPanel.add(balance1);
searchPanel.add(balance2);
panel.add(searchPanel);
return panel;
}
// 构建取钱存钱面板
private Panel createDrawPanel() {
Panel drawPanel = new Panel();
drawPanel.setLayout(null);
drawPanel.setSize(300, 300);
Panel top, middle, bottom;
top = new Panel();
top.setLayout(new GridLayout(1, 2, 5, 5));
top.setBounds(0, 55, 250, 30);
Label label = new Label("输入金额:", Label.RIGHT);
label.setFont(new Font("宋体", Font.PLAIN, 15));
label.setForeground(Color.RED);
money1 = new TextField();
top.add(label);
top.add(money1);
middle = new Panel();
middle.setBounds(30, 95, 250, 40);
submit1 = new Button("提交");
submit1.setForeground(Color.BLUE);
submit1.addActionListener(new Actions());
cancel1 = new Button("取消");
cancel1.setForeground(Color.BLUE);
cancel1.addActionListener(new Actions());
middle.add(submit1);
middle.add(cancel1);
bottom = new Panel();
bottom.setBounds(10, 135, 300, 200);
text1 = new TextArea("这是信息框!", 5, 30, TextArea.SCROLLBARS_NONE);
text1.setBackground(Color.WHITE);
text1.setForeground(Color.BLUE);
bottom.add(text1);
drawPanel.add(top);
drawPanel.add(middle);
drawPanel.add(bottom);
return drawPanel;
}
// 构建存钱面板
private Panel createDepositPanel() {
Panel drawPanel = new Panel();
drawPanel.setLayout(null);
drawPanel.setSize(300, 300);
Panel top, middle, bottom;
top = new Panel();
top.setLayout(new GridLayout(1, 2, 5, 5));
top.setBounds(0, 55, 250, 30);
Label label = new Label("输入金额:", Label.RIGHT);
label.setFont(new Font("宋体", Font.PLAIN, 15));
label.setForeground(Color.RED);
money2 = new TextField();
top.add(label);
top.add(money2);
middle = new Panel();
middle.setBounds(30, 95, 250, 40);
submit2 = new Button("提交");
submit2.setForeground(Color.BLUE);
submit2.addActionListener(new Actions());
cancel2 = new Button("取消");
cancel2.setForeground(Color.BLUE);
cancel2.addActionListener(new Actions());
middle.add(submit2);
middle.add(cancel2);
bottom = new Panel();
bottom.setBounds(10, 135, 300, 200);
text2 = new TextArea("这是信息框!", 5, 30, TextArea.SCROLLBARS_NONE);
text2.setBackground(Color.WHITE);
text2.setForeground(Color.BLUE);
bottom.add(text2);
drawPanel.add(top);
drawPanel.add(middle);
drawPanel.add(bottom);
return drawPanel;
}
// 更改密码面板
private Panel createModiPanel() {
Panel modiPanel = new Panel();
modiPanel.setLayout(null);
modiPanel.setSize(300, 300);
Panel top, middle, bottom;
top = new Panel();
top.setLayout(new GridLayout(3, 3, 5, 5));
top.setBounds(10, 35, 250, 100);
Label label1 = new Label("输入原始密码:", Label.RIGHT);
label1.setForeground(Color.RED);
label1.setFont(new Font("宋体", Font.PLAIN, 15));
password1 = new JPasswordField();
Label label2 = new Label("输入修改密码:", Label.RIGHT);
label2.setForeground(Color.RED);
label2.setFont(new Font("宋体", Font.PLAIN, 15));
password2 = new JPasswordField();
Label label3 = new Label("输入确认密码:", Label.RIGHT);
label3.setForeground(Color.RED);
label3.setFont(new Font("宋体", Font.PLAIN, 15));
password3 = new JPasswordField();
top.add(label1);
top.add(password1);
top.add(label2);
top.add(password2);
top.add(label3);
top.add(password3);
middle = new Panel();
middle.setBounds(0, 140, 300, 40);
submit3 = new Button("提交");
submit3.setForeground(Color.BLUE);
submit3.addActionListener(new Actions());
cancel3 = new Button("取消");
cancel3.setForeground(Color.BLUE);
cancel3.addActionListener(new Actions());
middle.add(submit3);
middle.add(cancel3);
bottom = new Panel();
bottom.setBounds(0, 175, 300, 200);
text3 = new TextArea("这是信息框!", 5, 30, TextArea.SCROLLBARS_NONE);
text3.setBackground(Color.WHITE);
text3.setForeground(Color.BLUE);
bottom.add(text3);
modiPanel.add(top);
modiPanel.add(middle);
modiPanel.add(bottom);
return modiPanel;
}
private void addToCards(Panel panel) {
cards = new Panel();
cards.setLayout(new CardLayout());
cards.add(createWelcomePanel(), texts[0]);
cards.add(createSearchPanel(), texts[1]);
cards.add(createDrawPanel(), texts[2]);
cards.add(createDepositPanel(), texts[3]);
cards.add(createModiPanel(), texts[4]);
panel.add(cards);
}
// 显示下标为index的卡片
public void showCard(int index) {
((CardLayout) cards.getLayout()).show(cards, texts[index]);
}
public Window(String id) {
username = id;
setLayout(null);
db = new DBase();
// 初始化左面板
left = new Panel();
left.setBounds(0, 0, 300, 300);
addToCards(left);
add(left);
// 初始化右面板
right = new Panel();
right.setBounds(320, 50, 100, 200);
right.setLayout(new GridLayout(5, 1, 5, 5));
search = new Button("查询余额");
search.setForeground(Color.BLUE);
search.addActionListener(new Actions());
draw = new Button("取钱");
draw.setForeground(Color.BLUE);
draw.addActionListener(new Actions());
deposit = new Button("存钱");
deposit.setForeground(Color.BLUE);
deposit.addActionListener(new Actions());
modi = new Button("修改密码");
modi.setForeground(Color.BLUE);
modi.addActionListener(new Actions());
comeback = new Button("返回");
comeback.setForeground(Color.blue);
comeback.addActionListener(new Actions());
right.add(search);
right.add(draw);
right.add(deposit);
right.add(modi);
right.add(comeback);
add(right);
setTitle("银行系统");
setBounds(400, 200, 520, 330);
setBackground(Color.lightGray);
setResizable(false);// 禁止调整框架的大小
// 匿名类关闭窗口
addWindowListener(new WindowAdapter() {
public
评论0