Java 多線程問題 with GUI

ChanKelvin 发布于 2012/12/09 13:09
阅读 475
收藏 0
這個是服務器--------------------------------
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MultithreadServer extends JFrame {
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;
HandleAClient task;
HandleAClient task1 ;
public static void main(String[] args) {
new MultithreadServer();

}

public MultithreadServer() { //constructor
   
    ServerSocket serversocket ;
    setLayout(new BorderLayout());
    add(new JScrollPane(jta), BorderLayout.CENTER);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(jtf, BorderLayout.CENTER);
    jtf.setHorizontalAlignment(JTextField.RIGHT);
 
    add(p, BorderLayout.SOUTH);
    jtf.addActionListener(new TextFieldListener());
    setTitle("Server");
    setSize(500,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    try {

    jta.append("MultiThread Started at "+ new Date() + '\n');
    int Clientno = 1;

    while(true){
    serversocket = new ServerSocket(8000);
    Socket socket = serversocket.accept();//SoServerSocket serversocket = new ServerSocket(8000);cket socket = new Socket("localhost", 8000);

    fromServer = new DataInputStream(socket.getInputStream());
    toServer = new DataOutputStream(socket.getOutputStream());
    
    task = new HandleAClient(socket);
    new Thread (task).start();
   // new Thread (task).sleep(500);
//    HandleAClient task1 = new HandleAClient(socket);
    jta.append("Client no is"+Clientno);
    Clientno ++;
    //jta.append("Client no is"+Clientno);
    }
//fromServer = new DataInputStream(socket.getInputStream());
// toServer = new DataOutputStream(socket.getOutputStream());
}
catch (Exception e) {
jta.append(e.toString() + '\n');
}
}
 private class TextFieldListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
    try {
    String a = (jtf.getText().trim());
    toServer.writeUTF(a);
    toServer.flush();
    jta.append("A: "+ a +"\n");
    }
    catch(IOException ex) {
    System.err.println(ex);
    }
    }
    }
class HandleAClient implements Runnable {
private Socket socket1;
//DataInputStream inputfromClient;
//DataOutputStream outputToClient;
public HandleAClient(Socket socket){
socket1 = socket;
}
public void run() {
DataInputStream inputfromClient;
DataOutputStream outputToClient;



jtf.addActionListener(new TextFieldListener());
}
}

}

---------------------------------------------------------------------------------------------------這個是客戶端....

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ThreadClient extends JFrame{

private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;

public static void main(String[] args) {
new ThreadClient();
}

public ThreadClient() {
JPanel p = new JPanel();
JLabel JL[] = new JLabel[6];
p.setLayout(new GridLayout(1,6));
jtf.setHorizontalAlignment(JTextField.RIGHT);
p.add(new Label("Time"));
p.add(new Label("Flight"));
p.add(new Label("where"));
p.add(new Label("status"));
p.add(new Label("status time"));
setLayout(new BorderLayout());
add(p, BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.CENTER);
// jtf.addActionListener(new TextFieldListener());

setTitle("Client");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Socket socket = new Socket();
InetSocketAddress isa = new InetSocketAddress("127.0.0.1",8000);
try {
socket.connect(isa,10000);
DataInputStream inputfromClient;
DataOutputStream outputToClient;

inputfromClient = new DataInputStream(socket.getInputStream());
outputToClient = new DataOutputStream(socket.getOutputStream());
while (true){
String b = inputfromClient.readUTF();
//outputToClient.flush();
jta.append("B:"+ b + "\n");
}
}
catch (IOException e){
jta.append(e.toString() + '\n');
}
}

}

問題是我建立了多線程,但是我啟動客戶端一次 成功傳送信息, 但再啟動 一共兩個客戶端, 只有第一個能夠收到信息,
如何可以實現兩個客戶端也收到信息

加载中
返回顶部
顶部