package com.mrsoft.ChatRoom;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.*;

/**
 * 连接服务器窗体
 * @param contentPane 内容面板
 * @param lblIP “服务器IP地址”标签
 * @param lblUserName “用户名”标签
 * @param tfIP “服务器IP地址”文本框
 * @param tfUserName “用户名”文本框
 * @param btnLink “连接服务器”按钮
 */
@SuppressWarnings("serial")// 通过注解抑制编译器警告
public class LinkServerFrame extends JFrame {

	private JPanel contentPane;
	private JLabel lblIP;
	private JLabel lblUserName;
	private JTextField tfIP;
	private JTextField tfUserName;
	private JButton btnLink;
	/**
	 * 连接服务器窗体的构造方法
	 */
	public LinkServerFrame() {
		setTitle("连接服务器");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 390, 150);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		lblIP = new JLabel("服务器IP地址：");
		lblIP.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblIP.setBounds(20, 15, 100, 15);
		contentPane.add(lblIP);
		
		tfIP = new JTextField();
		tfIP.setBounds(121, 13, 242, 21);
		contentPane.add(tfIP);
		tfIP.setColumns(10);
		
		lblUserName = new JLabel("用户名：");
		lblUserName.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblUserName.setBounds(60, 40, 60, 15);
		contentPane.add(lblUserName);
		
		tfUserName = new JTextField();
		tfUserName.setBounds(121, 42, 242, 21);
		contentPane.add(tfUserName);
		tfUserName.setColumns(10);
		
		btnLink = new JButton("连接服务器");
		btnLink.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnLink_actionPerformed(e);
			}
		});
		btnLink.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnLink.setBounds(140, 80, 120, 23);
		contentPane.add(btnLink);
	}

	public static void main(String[] args) {
		LinkServerFrame linkServerFrame = new LinkServerFrame();// 创建连接服务器窗体对象
		linkServerFrame.setVisible(true);// 显示连接服务器窗体
	}
	
	protected void do_btnLink_actionPerformed(ActionEvent e) {
		if (!tfIP.getText().equals("") && !tfUserName.getText().equals("")) {// 文本框中的内容不为空时
			dispose();// 销毁当前窗体
			ClientFrame clientFrame = new ClientFrame(tfIP.getText().trim(), tfUserName.getText().trim());// 创建客户端窗体对象并传参
			clientFrame.setVisible(true);// 显示客户端窗体
		} else {
			JOptionPane.showMessageDialog(null, "文本框里的内容不能为空！", "警告", JOptionPane.WARNING_MESSAGE);
		}
	}

}
