我想在客戶端和服務器之間來回發送一個類。想從客戶端運行Panel類。請任何人都可以告訴我該怎麼辦?發送對象的服務器客戶端
客戶端類:
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class Client
{
private static Socket socket = null;
public static void main (String args[]) throws IOException, ClassNotFoundException, EOFException
{
try {
socket = new Socket("localhost", 4444);
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost");
System.exit(1);
}
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
socket.close();
}
}
當我想讀面板對象從客戶那裏將是說沒有找到主類的錯誤。我如何從客戶端訪問面板類?
服務器類:
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException{
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
Socket socketOut = serverSocket.accept();
ObjectOutputStream oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
socketOut.close();
serverSocket.close();
}
}
面板類
import javax.swing.*;
import java.awt.*;
public class Panel extends JPanel {
public Panel()
{
JTextField n = new JTextField(10);
n.setText("Hello");
JButton q = new JButton("Who are you?");
setSize(300,300);
add(n);
add(q);
setLayout (new FlowLayout());
}
}
究竟是什麼ERR或消息?我在這裏只看到主要方法,沒有主要類......你是否將所有類複製到了客戶端和服務器類路徑? – lbalazscs
類問題已解決,但是如何從服務器客戶端進程使用另一個jFrame,jpanel類 –