📄 mainpanel.java
字号:
package tarena.bank.view;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainPanel extends JFrame implements Runnable, ActionListener {
private JLabel label1, label2, label3;
private JButton button1, button2;
private JTextField textField1;
private JPasswordField passwordField;
private DataInputStream inStream = null;
private DataOutputStream outStream = null;
private Dimension screemSize = Toolkit.getDefaultToolkit().getScreenSize();
public MainPanel() {
super("银行账户管理系统");
getContentPane().setBackground(Color.DARK_GRAY);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 500, 375);
label1 = new JLabel("欢迎使用BAM在线系统");
label1.setBackground(Color.DARK_GRAY);
label1.setHorizontalTextPosition(SwingConstants.CENTER);
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setForeground(Color.BLUE);
label1.setFont(new Font("幼圆", Font.PLAIN, 28));
label1.setBounds(81, 23, 337, 47);
this.add(label1);
label2 = new JLabel("用户账号:");
label2.setBounds(120, 101, 105, 33);
label2.setFont(new Font("@幼圆", Font.PLAIN, 18));
label2.setBackground(Color.LIGHT_GRAY);
label2.setForeground(Color.blue);
this.add(label2);
label3 = new JLabel("用户密码:");
label3.setFont(new Font("@幼圆", Font.PLAIN, 18));
label3.setBackground(Color.LIGHT_GRAY);
label3.setForeground(Color.blue);
label3.setBounds(120, 140, 105, 33);
this.add(label3);
textField1 = new JTextField(8);
textField1.setBackground(Color.LIGHT_GRAY);
textField1.setFont(new Font("@幼圆", Font.PLAIN, 16));
textField1.setBounds(243, 108, 130, 26);
this.add(textField1);
passwordField = new JPasswordField();
passwordField.setBackground(Color.LIGHT_GRAY);
passwordField.setFont(new Font("@幼圆", Font.PLAIN, 16));
passwordField.setBounds(243, 140, 130, 26);
this.add(passwordField);
button1 = new JButton("登录");
button1.setFont(new Font("@幼圆", Font.PLAIN, 18));
button1.setBackground(Color.LIGHT_GRAY);
button1.setForeground(Color.MAGENTA);
button1.setBounds(129, 197, 112, 33);
button1.addActionListener(this);
this.add(button1);
button2 = new JButton("注册");
button2.setFont(new Font("@幼圆", Font.PLAIN, 18));
button2.setBackground(Color.LIGHT_GRAY);
button2.setForeground(Color.MAGENTA);
button2.setBounds(256, 197, 112, 33);
button2.addActionListener(this);
this.add(button2);
this.setVisible(true);
this.setLocation((screemSize.width - 400) / 2,
(screemSize.height - 400) / 2);
}
public static void main(String[] args) {
try {
MainPanel frame = new MainPanel();
frame.start();
} catch (Exception e) {
e.printStackTrace();
}
}
//发送消息的方法
private void send(String str) {
try {
outStream.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
send("Loading" + textField1.getText() + "/"
+ String.valueOf(passwordField.getPassword()));
} else if (e.getSource() == button2) {
new RegisterPanel().start();
}
}
public void start() {
Thread thread = new Thread(this);
// 打开线程
thread.start();
}
public void run() {
try {
Socket socket = new Socket("127.0.0.1", 8888);
// System.out.println(socket);
outStream = new DataOutputStream(socket.getOutputStream());
inStream = new DataInputStream(socket.getInputStream());
while (true) {
String str = inStream.readUTF();// 读取服务器发来的信息
if (str.substring(0, 4).equals("登录成功")) {
List l = update(str.substring(4));
// new 登录后的界面
new OperationPanel((String) l.get(0), (String) l.get(1),
(String) l.get(2), (String) l.get(3), (String) l
.get(4), (String) l.get(5), (String) l
.get(6)).start();
socket.close();
this.dispose();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//该方法用于解析服务器发送来的登录成功消息,从中分离出new OperationPanel()中的7个参数
private List update(String s) {
List<String> list = new ArrayList<String>();
int index = 0;
if (s.charAt(s.length() - 2) == '0') {
int tag = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/') {
tag++;
list.add(s.substring(index, i));
if (tag == 4) {
list.add("0.0");
list.add("0.0");
}
index = i + 1;
}
}
list.remove(6);
list.add("储蓄账户");
} else if (s.charAt(s.length() - 2) == '1') {
int tag = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/') {
tag++;
list.add(s.substring(index, i));
if (tag == 5) {
list.add("0.0");
}
index = i + 1;
}
}
list.remove(6);
list.add("信用账户");
} else if (s.charAt(s.length() - 2) == '2') {
int tag = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/') {
tag++;
list.add(s.substring(index, i));
if (tag == 4) {
list.add("0.0");
}
index = i + 1;
}
}
list.remove(6);
list.add("贷款储蓄账户");
} else if (s.charAt(s.length() - 2) == '3') {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/') {
list.add(s.substring(index, i));
index = i + 1;
}
}
list.remove(6);
list.add("贷款信用账户");
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -