2012-09-06 86 views
0

我想在客戶端和服務器之間來回發送一個類。想從客戶端運行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()); 
    } 
} 
+2

究竟是什麼ERR或消息?我在這裏只看到主要方法,沒有主要類......你是否將所有類複製到了客戶端和服務器類路徑? – lbalazscs

+0

類問題已解決,但是如何從服務器客戶端進程使用另一個jFrame,jpanel類 –

回答

0

要通過你需要序列化對象插座之間發送對象到不同的進程。所以面板類需要實現Serializable接口:http://java.sun.com/developer/technicalArticles/Programming/serialization/

至於主類沒有找到的東西。我假設這兩個類必須是兩個獨立的進程。在這種情況下,您可能會發現如果您在IDE中構建這個主類定義是兩個項目的服務器。你可能需要糾正這一點。確保有兩個獨立的構建結果和兩個獨立的主類。

+0

感謝您的幫助。但我可以設法通過創建一個對象來顯示Jframe窗口。但是Jframe類中有一些操作。我如何運行它們? –

+0

嘗試對它進行序列化,這會泄漏整個對象進行傳輸,並在另一端進行重建。如果你創建了一個對象,你會失去你所做的任何改變。 – feldoh

+0

ok done 我有兩個或更多的JFrame類一個接一個地工作。我是否必須在客戶端類中編寫所有類名?我爲一個班級使用了setvisible。剩下的課程在這堂課之後相繼出現。但不要在服務器端。我能做什麼??? –

0

服務器類:

import java.net.*; 
import java.io.*; 

class ServerThread extends Thread { 
    private Socket socket = null; 

    public ServerThread(Socket socket) { 
    super("ServerThread"); 
    this.socket = socket; 
    } 

    public void run() { 

    try { 
      ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ; 
      EagleEye y = new EagleEye(); 
      Object object = y; 
      oos.writeObject(object); 
      oos.flush(); 
      oos.close(); 
      socket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

public class Server { 
    public static void main(String[] args) throws IOException { 
     ServerSocket serverSocket = null; 
     boolean listening = true; 

     try { 
      serverSocket = new ServerSocket(4444); 
     } catch (IOException e) { 
      System.err.println("Could not listen on port: 4444."); 
      System.exit(1); 
     } 

     while (listening) 
     new ServerThread(serverSocket.accept()).start(); 

     serverSocket.close(); 
    } 
} 

客戶端類:

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()); 
     EagleEye y = (EagleEye)ois.readObject(); 
     socket.close(); 
    } 
} 

鷹眼(正常JFrame類):

public class EagleEye extends JFrame implements Serializable { 
    private JLabel label1; 
    private Icon photo; 

    public EagleEye(){ 
     super("EAGLE EYE"); 
     photo= new ImageIcon(getClass().getResource("Untitled.jpg")); 
     label1= new JLabel(); 
     label1.setIcon(photo); 
     label1.setBounds(0, 0, 800, 600); 
     add(label1); 

     label1.addMouseListener(new MouseAdapter(){ 
      public void mousePressed(MouseEvent e) 
      { 
       int x= e.getX(); 
       int y=e.getY(); 
       System.out.println(); 
       if(x>120 && x<150 && y>80 && y<110){ 
        drawCircle(667,280); 
       } 
       if(x>174 && x<214 && y>125 && y<170){ 
        drawCircle(667,280); 
       } 
       if(x>250 && x<270 && y>150 && y<240){ 
        drawCircle(667,320); 
       } 
       if(x>375 && x<402 && y>75 && y<112){ 
        drawCircle(670,359); 
       } 
       if(x>440 && x<490 && y>180 && y<215){ 
        drawCircle(675,397); 
       } 
       if(x>535 && x<615 && y>110 && y<140){ 
        drawCircle(670,437); 
       } 
       removeMouseListener(this); 
      } 
     }); 

     setSize(800,600); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void drawCircle(int x, int y) { 
     Graphics g = getGraphics(); 
     g.setColor(Color.RED); 
     g.drawOval(x,y,70,30); 
    } 
    public static void main(String[] args){ 
     EagleEye e= new EagleEye(); 
    } 
} 
相關問題