/**
*
*/
package GUI;
import javax.swing.*;
import Model.ListenClient;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
/**
* @author 于秀芬
* @version 1.0
*/
public class ServerGUI extends JFrame implements ActionListener, Runnable{
/**
*
*/
private static final long serialVersionUID = 994668581438423440L;
private JLabel label1 = new JLabel("服务器状态信息",JLabel.CENTER);
private JLabel label2 = new JLabel("服务端口:",JLabel.RIGHT);
private JLabel label3 = new JLabel("服务状态:",JLabel.RIGHT);
private JLabel label4 = new JLabel("客户端连接数:",JLabel.RIGHT);
private JLabel label5 = new JLabel("昵称:",JLabel.RIGHT);
private JLabel label6 = new JLabel("启动服务器:");
private JTextField PortField = new JTextField(5);
private JTextField ServerState = new JTextField(5);
private JTextField ConnectNumbers = new JTextField(5);
private JTextField NameField = new JTextField(5);
private JButton StartServer = new JButton("启动服务");
private JButton Send = new JButton("发送系统消息");
private JButton Close = new JButton("关闭程序");
private JTextArea ShowArea = new JTextArea();
private JTextArea EditArea = new JTextArea();
private JScrollPane scroll1 = new JScrollPane(ShowArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
private JScrollPane scroll2 = new JScrollPane(EditArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
private static ServerSocket svr;
private static ServerGUI sgui;
private static Vector<ListenClient> v_clientThread = new Vector<ListenClient>(10);
private int id = 0; // 用来记录客户端的连接数
ServerGUI() {
setTitle("服务器端");
setSize(500,400);
setLocation(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridBagLayout gblayout = new GridBagLayout();
pane.setLayout(gblayout);
GridBagConstraints c = new GridBagConstraints();
PortField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent kev) {
if(kev.getKeyChar()<0x30||kev.getKeyChar()>0x39) {
kev.consume();
}
}
});
StartServer.addActionListener(this);
Send.addActionListener(this);
Close.addActionListener(this);
c.insets = new Insets(5,5,5,5);
label1.setBorder(BorderFactory.createLineBorder(Color.red));
ShowArea.setBorder(BorderFactory.createLineBorder(Color.black));
EditArea.setBorder(BorderFactory.createLineBorder(Color.black));
ServerState.setEditable(false);
ConnectNumbers.setEditable(false);
NameField.setEditable(false);
ShowArea.setEditable(false);
ShowArea.setLineWrap(true);
ShowArea.setWrapStyleWord(true);
EditArea.setLineWrap(true);
EditArea.setWrapStyleWord(true);
NameField.setText("服务器");
ServerState.setText("服务器未启动");
ConnectNumbers.setText("0");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 6;
c.gridwidth = 7;
c.weightx = 8;
c.weighty = 6;
gblayout.setConstraints(scroll1, c);
pane.add(scroll1);
c.gridx = 0;
c.gridy = 6;
c.gridheight = 2;
c.gridwidth = 7;
c.weightx = 8;
c.weighty = 2;
gblayout.setConstraints(scroll2, c);
pane.add(scroll2);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 8;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 2;
c.weightx = 2;
c.weighty = 1;
gblayout.setConstraints(label1, c);
pane.add(label1);
c.gridx = 8;
c.gridy = 1;
c.gridheight = 1;
c.gridwidth = 1;
c.weightx = 1;
c.weighty = 1;
gblayout.setConstraints(label2, c);
pane.add(label2);
c.gridx = 9;
c.gridy = 1;
gblayout.setConstraints(PortField, c);
pane.add(PortField);
c.gridx = 8;
c.gridy = 2;
gblayout.setConstraints(label3, c);
pane.add(label3);
c.gridx = 9;
c.gridy = 2;
gblayout.setConstraints(ServerState, c);
pane.add(ServerState);
c.gridx = 8;
c.gridy = 3;
gblayout.setConstraints(label4, c);
pane.add(label4);
c.gridx = 9;
c.gridy = 3;
gblayout.setConstraints(ConnectNumbers, c);
pane.add(ConnectNumbers);
c.gridx = 8;
c.gridy = 4;
gblayout.setConstraints(label5, c);
pane.add(label5);
c.gridx = 9;
c.gridy = 4;
gblayout.setConstraints(NameField, c);
pane.add(NameField);
c.gridx = 8;
c.gridy = 5;
gblayout.setConstraints(label6, c);
pane.add(label6);
c.gridx = 9;
c.gridy = 5;
gblayout.setConstraints(StartServer, c);
pane.add(StartServer);
c.anchor = GridBagConstraints.SOUTH;
c.gridx = 9;
c.gridy = 6;
gblayout.setConstraints(Send, c);
pane.add(Send);
c.gridx = 9;
c.gridy = 7;
gblayout.setConstraints(Close, c);
pane.add(Close);
setVisible(true);
}
public static void main(String[] args) {
sgui = new ServerGUI();
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if(source == StartServer) {
if(!PortField.getText().equals("")) {
int port = Integer.parseInt(PortField.getText());
if(port > 65535) {
JOptionPane.showMessageDialog(this, "服务器端口范围是0-65535,请重新设置!");
PortField.setText("");
return;
}
try {
svr = new ServerSocket(port);
Thread t = new Thread(sgui);
t.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StartServer.setEnabled(false);
ServerState.setText("服务器已启动");
} else {
JOptionPane.showMessageDialog(this, "服务器端口号为空,无法启动");
return;
}
}
if(source == Send) {
String str = EditArea.getText();
if(v_clientThread.size() == 0) {
JOptionPane.showMessageDialog(this, "无客户端连接到服务器,无法发送信息");
EditArea.setText("");
return;
}
if(str.equals("")) {
JOptionPane.showMessageDialog(this, "发送信息不能为空!");
return;
}
if(v_clientThread.size() == 0) {
JOptionPane.showMessageDialog(this, "无客户端连接到服务器,发送失败");
return;
}
for(int i = 0; i < v_clientThread.size(); i++) {
try {
ListenClient lc = (ListenClient)v_clientThread.elementAt(i);
lc.out.writeUTF("服务器说:\n" + str + "\n\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ShowArea.append("服务器说:\n" + str + "\n\n");
EditArea.setText("");
}
if(source == Close) {
System.exit(0);
}
}
public void run() {
while(true) {
try {
Socket soc = svr.accept();
ListenClient lc = new ListenClient(++id,soc,v_clientThread,ShowArea,ConnectNumbers);
v_clientThread.add(lc);
lc.start();
ConnectNumbers.setText("" + v_clientThread.size());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}