package workspace.liaotian;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.Socket;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
/*这个类是服务器端的UI*/
public class ServerUI extends JFrame {
public static void main(String[] args) {
ServerUI serverUI = new ServerUI();
}
public JButton btStart;//启动服务器
public JButton btSend;//发送信息按钮
public JTextField tfSend;//需要发送的文本信息
public JTextArea taShow;//信息展示
public Server server;//用来监听客户端连接
static List<Socket> clients;//保存连接到服务器的客户端
private JTextField textField;
private JButton btnFasong;
private JTextField textField_1;
public JTextField count;
public ServerUI() {
super("服务器端");
btSend = new JButton("发送信息");
btSend.setBounds(431, 395, 91, 23);
taShow = new JTextArea();
getContentPane().setLayout(null);
final JScrollPane sp = new JScrollPane();
sp.setBounds(0, 10, 414, 375);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setViewportView(this.taShow);
this.taShow.setEditable(false);
getContentPane().add(sp);
btStart = new JButton("启动服务");
btStart.setBounds(431, 111, 98, 48);
getContentPane().add(btStart);
tfSend = new JTextField();
tfSend.setBounds(10, 395, 404, 33);
getContentPane().add(tfSend);
tfSend.setColumns(10);
getContentPane().add(tfSend); //文本
getContentPane().add(btSend);
JLabel lblNewLabel = new JLabel("连接数量:");
lblNewLabel.setBounds(444, 199, 78, 33);
getContentPane().add(lblNewLabel);
count = new JTextField();
count.setBounds(443, 242, 66, 45);
getContentPane().add(count);
count.setColumns(10);
//为发送信息加监听器
btSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
server.sendMsg(tfSend.getText()); //从输入框得到输入信息发送出去
tfSend.setText("");
System.out.println(tfSend.getText()+"*****");
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "提示",
JOptionPane.YES_NO_OPTION);
if (a == 1) {
server.closeServer();
System.exit(0); // 关闭
}
}
});
//为启动服务加监听器
btStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
server = new Server(ServerUI.this);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(565, 477);
this.setLocation(100, 200);
this.setVisible(true);
}
}