package client;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.*;
import java.util.zip.CRC32;
import javax.swing.*;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 设置观感
String aLookAndFeelString = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
try {
UIManager.setLookAndFeel(aLookAndFeelString);
} catch (Exception e) {
e.printStackTrace();
}
// 显示登录界面
LoginFrame frame = new LoginFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MSFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public MSFrame() {
// 获取屏幕分辨率
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
// 设置主窗体属性
setLocation(screenWidth / 3, screenHeight / 4);
setSize(screenWidth / 3 + 30, screenHeight / 2);
setResizable(false);
setTitle("人力资源管理系统");
java.net.URL imgUrl = MSFrame.class.getResource("./Client.jpg");
ImageIcon imgIcon = new ImageIcon(imgUrl);
Image img = imgIcon.getImage();
setIconImage(img);
// 添加功能菜单
JPanel menuJPanel = new JPanel(new GridLayout(3, 1));
add(menuJPanel, BorderLayout.NORTH);
JPanel firstJPanel = new JPanel();
menuJPanel.add(firstJPanel);
// 新人入职按钮
addJB = new JButton("新人入职");
addJB.addActionListener(new AddAction());
firstJPanel.add(addJB);
// 换部门按钮
changeDeptJB = new JButton("调换部门");
changeDeptJB.addActionListener(new ChangeDept());
firstJPanel.add(changeDeptJB);
// 职位调整按钮
changePosJB = new JButton("职位调整");
changePosJB.addActionListener(new ChangePos());
firstJPanel.add(changePosJB);
// 工资调整按钮
changeSalayJB = new JButton("工资调整");
changeSalayJB.addActionListener(new ChangeSalary());
firstJPanel.add(changeSalayJB);
JPanel secondJPanel = new JPanel();
menuJPanel.add(secondJPanel);
// 查询单人按钮
searchSinglJB = new JButton("单人查询");
searchSinglJB.addActionListener(new SearchSingle());
secondJPanel.add(searchSinglJB);
// 查询多人按钮
searchMultiJB = new JButton("多人查询");
searchMultiJB.addActionListener(new SearchMulit());
secondJPanel.add(searchMultiJB);
// 查看所有人名单按钮
searchAllJB = new JButton("查看全部人员");
secondJPanel.add(searchAllJB);
searchAllJB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
out.println("SEARCHALL");
String resultString = in.nextLine();
String[] result = resultString.split("#");
infoJTextArea.setText("已连接到服务器……\n");
for (int i = 0; i < result.length; i++) {
infoJTextArea.append(result[i]);
infoJTextArea.append("\n");
}
}
});
// 假期查询按钮
searchHoliday = new JButton("假期查询");
searchHoliday.addActionListener(new SearchHoliday());
secondJPanel.add(searchHoliday);
// 离职按钮
fireJB = new JButton("员工离职");
fireJB.addActionListener(new Fire());
firstJPanel.add(fireJB);
// 退出系统按钮
exitJB = new JButton("退出系统");
exitJB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
secondJPanel.add(exitJB);
// 服务器的地址及端口
JPanel thirdJPanel = new JPanel();
menuJPanel.add(thirdJPanel);
JLabel aJLabel = new JLabel("服务器IP:");
ipJTextField = new JTextField("192.168.12.37", 10);
thirdJPanel.add(aJLabel);
thirdJPanel.add(ipJTextField);
JLabel bJLabel = new JLabel("端口:");
portJTextField = new JTextField("8189", 6);
thirdJPanel.add(bJLabel);
thirdJPanel.add(portJTextField);
connectJButton = new JButton("连接");
connectJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ip = ipJTextField.getText();
port = Integer.valueOf(portJTextField.getText());
SocketChannel channel = null;
try {
channel = SocketChannel
.open(new InetSocketAddress(ip, port));
} catch (IOException e1) {
// TODO Auto-generated catch block
infoJTextArea.append("连接服务器失败!\n");
}
in = new Scanner(channel);
OutputStream outsStream = Channels.newOutputStream(channel);
out = new PrintWriter(outsStream, true);
try {
out.println(InetAddress.getLocalHost().toString());
if (in.nextLine().equals("TRUE")) {
infoJTextArea.setText(null);
infoJTextArea.append("已连接到服务器……\n");
}
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
infoJTextArea.append("连接服务器失败!\n");
}
}
});
thirdJPanel.add(connectJButton);
disconnectJButton = new JButton("断开");
disconnectJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
out.println("DISCONNECT");
try {
out.println(InetAddress.getLocalHost().toString());
} catch (UnknownHostException e1) {
infoJTextArea.append("断开连接失败!");
}
if (in.nextLine().equals("DISCONNECT"))
infoJTextArea.append("\n已与服务器断开连接……\n");
}
});
thirdJPanel.add(disconnectJButton);
// 显示信息文本域
infoJTextArea = new JTextArea();
infoJTextArea.setText("未连接服务器……\n");
// infoJTextArea.setBackground(Color.cyan);
infoJTextArea.setEditable(false);
aJScrollPane = new JScrollPane(infoJTextArea);
add(aJScrollPane);
}
private JButton addJB;
private JButton changeDeptJB;
private JButton changePosJB;
private JButton changeSalayJB;
private JButton searchSinglJB;
private JButton searchMultiJB;
private JButton searchAllJB;
private JButton searchHoliday;
private JButton fireJB;
private JButton exitJB;
private JTextArea infoJTextArea;
private JScrollPane aJScrollPane;
private JTextField ipJTextField;
private JTextField portJTextField;
private JButton connectJButton;
private JButton disconnectJButton;
private String ip = null;
private int port = 0;
private Scanner in;
private PrintWriter out;
/*
* 以下为各按钮监听器
*/
// “新入职员”按钮的监听器
private class AddAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
// 如果是第一次调用,构造一个新人入职窗口
if (aAddFrame == null)
aAddFrame = new AddFrame();
if (aAddFrame.showAddFrame(MSFrame.this)) {
// 添加一个员工并返回提示信息
try {
out.println("ADDNEW");
out.println(aAddFrame.getInfoString());
} catch (Exception e) {
// TODO: handle exception
infoJTextArea.setText("未连接服务器!\n");
}
infoJTextArea.append(in.nextLine());
}
}
}
// “换部门”按钮的监听器
private class ChangeDept implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (aChangeDeptFrame == null)
aChangeDeptFrame = new ChangeDeptFrame();
if (