package com.demo; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.util.List; import java.awt.event.ActionEvent; import javax.swing.JTextArea; public class StartService extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField textField; private JTextField textField_1; private static ServiceSoketServerListenerThread sssl=null; private JTextArea textArea; private JButton button_2; private JTextField textField_2; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { StartService frame = new StartService(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public StartService() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 410, 469); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblIp = new JLabel("ip"); lblIp.setBounds(10, 62, 54, 15); contentPane.add(lblIp); JLabel label = new JLabel("端口"); label.setBounds(213, 62, 54, 15); contentPane.add(label); textField = new JTextField(); textField.setText("127.0.0.1"); textField.setBounds(59, 59, 66, 21); contentPane.add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setText("8080"); textField_1.setColumns(10); textField_1.setBounds(275, 59, 66, 21); contentPane.add(textField_1); JButton button = new JButton("开启服务"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (button.getText().equals("开启服务")) { sssl = new ServiceSoketServerListenerThread(Integer.valueOf(textField_1.getText()), textArea); sssl.start(); button.setText("关闭服务"); } else { sssl.stopService(); button.setText("开启服务"); } } }); button.setBounds(88, 120, 166, 23); contentPane.add(button); textArea = new JTextArea(); textArea.setBounds(31, 177, 317, 130); JScrollPane scrollPane_1 = new JScrollPane(); scrollPane_1.setBounds(31, 177, 317, 130); scrollPane_1.setViewportView(textArea); contentPane.add(scrollPane_1); contentPane.add(scrollPane_1); button_2 = new JButton("向所有客户端发消息"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { List<User> socketlist = ServiceSoketServerListenerThread.getSocketlist(); for (User user : socketlist) { try { user.getSocket().getOutputStream().write(("服务器:"+textField_2.getText()).getBytes()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } /*for (Socket socket : socketList) { try { socket.getOutputStream().write(("服务器:"+textField_2.getText()).getBytes()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { DataOutputStream dis=new DataOutputStream(socket.getOutputStream()); dis.writeUTF("服务器:"+textField_2.getText()); } catch (IOException e1) { socketList.remove(socket); System.out.println("对方退出了!我从List里面去掉了!"); e1.printStackTrace(); } }*/ } }); button_2.setBounds(59, 398, 174, 23); contentPane.add(button_2); textField_2 = new JTextField(); textField_2.setBounds(59, 332, 208, 21); contentPane.add(textField_2); textField_2.setColumns(10); } }