📄 servestart.java
字号:
package net.yangzhe.chatSocket;
import java.awt.BorderLayout;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JList;
public class ServeStart extends JFrame {
private Thread t,t2;
private JPanel jContentPane = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JScrollPane jScrollPane1 = null;
private JTextPane jTextPane1 = null;
private JLabel jLabel = null;
private JButton jButton1 = null;
public JList jList = null;
/**
* This is the default constructor
* @throws IOException
*/
public ServeStart() throws IOException {
super();
initialize();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//startServer();
}
private void startServer() throws IOException{
ServerSocket server=new ServerSocket(5678);
ChatServer.isWork=true;
while(true){
//transfer location change Single User or Multi User
ChatServer s=new ChatServer(server.accept(),this);
s.start();
ChatServer.serverCollection.add(s);
if(!ChatServer.isWork){
break;
}
}
server.close();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("Chat服务器");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new java.awt.Rectangle(10,108,56,47));
jLabel.setText("在线名单");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJScrollPane1(), null);
jContentPane.add(jLabel, null);
jContentPane.add(getJButton1(), null);
}
return jContentPane;
}
private void endServer() {
// TODO Auto-generated method stub
ChatServer.end();
ChatServer.serverCollection.clear();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(ChatServer s:ChatServer.serverCollection){
s.stop();
}
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new java.awt.Rectangle(197,136,86,23));
jButton.setText("关闭服务");
jButton.setEnabled(false);
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
endServer();
try {//把server总进程关闭
Client c = new Client(InetAddress.getLocalHost());
c.sendNews("%end");
c.sendNews("%end");
if(c.getNews()!=null)
c.server.close();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] clear={""};
jList.setListData(clear);
jButton1.setEnabled(true);
jButton.setEnabled(false);
t.stop();
t2.stop();
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(14,7,270,86));
jScrollPane.setViewportView(getJTextPane1());
}
return jScrollPane;
}
/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane1() {
if (jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(new java.awt.Rectangle(74,104,109,54));
jScrollPane1.setViewportView(getJList());
}
return jScrollPane1;
}
/**
* This method initializes jTextPane1
*
* @return javax.swing.JTextPane
*/
private JTextPane getJTextPane1() {
if (jTextPane1 == null) {
jTextPane1 = new JTextPane();
jTextPane1.setText("这里是服务器状态栏");
}
return jTextPane1;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new java.awt.Rectangle(195,106,88,24));
jButton1.setText("开启服务");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
t = new Thread(new Runnable(){
public void run() {
try {
startServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
t2 = new Thread(new Runnable(){
public void run() {
while(true){
Vector <String> test=ChatServer.getUserList();
jList.setListData(test);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t2.start();
jButton.setEnabled(true);
jButton1.setEnabled(false);
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton1;
}
/**
* This method initializes jList
*
* @return javax.swing.JList
*/
private JList getJList() {
if (jList == null) {
jList = new JList();
}
return jList;
}
public static void main(String[] args) throws IOException{
ServeStart s = new ServeStart();
s.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -