将客户端请求单独分出来,方便直接请求。
public class WeChatClientUtil
{
private static final String HOST="http://localhost:8000";
private static final SimpleClient client=new SimpleClient(HOST);
private WeChatClientUtil(){}
public static String makeRequest(String cmd,Map<String,String> params)
{
Request request = new Request(cmd);
for(Map.Entry<String,String> entry:params.entrySet())
{
request.addParam(entry.getKey(), entry.getValue());
}
try
{
return client.makeRequest(request);
}catch(Exception e)
{
return e.getMessage();
}
客户端的代码
public class WeChatClient extends GraphicsProgram {
JPanel northPanel;
JPanel westPanel;
JPanel eastPanel;
JPanel centerPanel;
JLabel nameJLabel;
JLabel bottomLabel;
JLabel statusJLabel;
JLabel friend;
JLabel ageJLabel;
JButton clearButton;
JButton queryButton;
JButton nameJButton;
JButton ageJButton;
JButton statusJButton;
JButton pictureJButton;
JButton friendsJButton;
JTextField nameTextField;
JTextField updateStatus;
JTextField updatePicture;
JTextField updateFriends;
JTextField updateage;
private Map<String, String> map = null; // 客户端请求参数
public static final String SUCCESS_MSG = "success";
private JPanel southPanel; // 账户管理
Font font = new Font("宋体", Font.PLAIN, 25);
Font font1 = new Font("宋体", Font.PLAIN + Font.BOLD, 25);
public void init() {
setTitle("朋友O客户端");
northPanel = getRegionPanel(NORTH);
westPanel = getRegionPanel(WEST);
eastPanel = getRegionPanel(EAST);
southPanel = getRegionPanel(SOUTH);
centerPanel = getRegionPanel(CENTER);
centerPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
nameJLabel = new JLabel("姓名");
northPanel.add(nameJLabel);
nameTextField = new JTextField(10);
northPanel.add(nameTextField);
nameJButton = new JButton("添加账户");
northPanel.add(nameJButton);
clearButton = new JButton("删除账户");
northPanel.add(clearButton);
queryButton = new JButton("查询账户");
northPanel.add(queryButton);
updateStatus = new JTextField(10);
statusJButton = new JButton("更新状态");
updatePicture = new JTextField(10);
pictureJButton = new JButton("更新图片");
updateFriends = new JTextField(10);
friendsJButton = new JButton("添加好友");
updateage = new JTextField(10);
ageJButton = new JButton("更新年龄");
westPanel.add(updateStatus);
westPanel.add(statusJButton);
westPanel.add(updatePicture);
westPanel.add(pictureJButton);
westPanel.add(updateFriends);
westPanel.add(friendsJButton);
westPanel.add(updateage);
westPanel.add(ageJButton);
friend = new JLabel("好友列表");
friend.setFont(font);
eastPanel.add(friend);
statusJLabel = new JLabel();
ageJLabel = new JLabel();
map = new HashMap<>();
}
public void run() {
showAccountPanel();
// display();
}
public void showAccountPanel() {
bottomLabel = new JLabel("", JLabel.CENTER);
bottomLabel.setForeground(Color.BLACK);
bottomLabel.setFont(font);
statusJLabel.setFont(font1);
southPanel.setLayout(new BorderLayout(0, 5));
southPanel.add(bottomLabel);
registeredAccount();
updateStatus();
friend();
updateAge();
picture();
findUsers();
remove();
}
// 添加账户
public void registeredAccount() {
nameJButton.addActionListener(e -> {
String out;
String name = nameTextField.getText().trim();
if ("".equals(name)) {
out = "用户名为空";
} else if (name.contains(",") ||