2010-01-13 35 views
0

抱歉張貼了很多代碼!!我不知道爲什麼我的ListFrame不起作用??? 這些是類。首先我運行MainServer,然後在另一個包中運行MainFrame,然後通過插入正確的用戶名和密碼來顯示Listframe,但是我單擊菜單欄或列表或刪除按鈕,但沒有什麼會發生。請幫幫我。Swing問題!(顯示框架時出現問題)

MainSerevr類:

public class MainServer { 

static Socket client = null; 
static ServerSocket server = null; 

public static void main(String[] args) { 
    System.out.println("Server is starting..."); 
    System.out.println("Server is listening..."); 

    try { 
     server = new ServerSocket(5050); 
    } catch (IOException ex) { 
     System.out.println("Could not listen on port 5050"); 
     System.exit(-1); 

    } 
    try { 
     client = server.accept(); 
     System.out.println("Client Connected..."); 
    } catch (IOException e) { 
     System.out.println("Accept failed: 5050"); 
     System.exit(-1); 
    } 
    try { 
     BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream())); 

     boolean done = false; 
     String line; 
     while (!done) { 
      line = streamIn.readLine(); 
      if (line.equalsIgnoreCase(".bye")) { 
       done = true; 
      } else { 
       System.out.println("Client says: " + line); 
      } 
     } 

     streamIn.close(); 
     client.close(); 
     server.close(); 
    } catch (IOException e) { 
     System.out.println("IO Error in streams " + e); 
    } 
}} 

ListFrame:

public class ListFrame extends javax.swing.JFrame implements PersonsModelChangeListener { 

    private InformationClass client; 
    private static DefaultListModel model = new DefaultListModel(); 
    private ListSelectionModel moDel; 

    /** Creates new form ListFrame */ 
    public ListFrame(InformationClass client) { 
     initComponents(); 
     this.client = client; 
     jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 


     fillTable(); 
     Manager.addListener(this); 
    } 

    private void deleteAPerson() { 
     int index = jList1.getSelectedIndex(); 
     String yahooId = (String) jList1.getSelectedValue(); 
     model.remove(index); 
     Manager.removeApersonFromSQL(yahooId); 
     int size = model.getSize(); 
     if (size == 0) { 
      jButton1.setEnabled(false); 
     } else { 
      if (index == size) { 
       index--; 
      } 
      jList1.setSelectedIndex(index); 
      jList1.ensureIndexIsVisible(index); 

     } 
    }      



    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {           
     AddAPerson frame = new AddAPerson(client); 
     frame.setVisible(true); 


    }           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     deleteAPerson(); 
    }           

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {          
     MainClient.setText(""); 
     MainClient.runAClient(); 
     ChatFrame frame = new ChatFrame(client); 
     frame.setVisible(true); 
    } 
    public void fillTable() { 
    try { 
     List<InformationClass> list = null; 
     list = Manager.getClientListFromMySQL(); 
     if (list == null) { 

      JOptionPane.showMessageDialog(this, "You should add a person to your list", "Information", JOptionPane.OK_OPTION); 
      return; 
     } else { 


      for (int i = 0; i < list.size(); i++) { 
       InformationClass list1 = list.get(i); 
       model.add(i, list1.getId()); 
      } 

      jList1.setModel(model); 
     } 


    } catch (SQLException ex) { 
     Logger.getLogger(ListFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

MainClient類:

public class MainClient { 


private static InformationClass info = new InformationClass(); 
private static Socket c; 
private static String text; 

public static String getText() { 
    return text; 
} 

public static void setText(String text) { 
    MainClient.text = text; 
} 

private static PrintWriter os; 
private static BufferedReader is; 
static boolean closed = false; 

/** 
* @param args the command line arguments 
*/ 
public static void runAClient() { 
    try { 
     c = new Socket("localhost", 5050); 

     os = new PrintWriter(c.getOutputStream()); 
     is = new BufferedReader(new InputStreamReader(c.getInputStream())); 

     String teXt = getText(); 
     os.println(teXt); 
     if(c!=null&& is!=null&&os!=null){ 
     String line = is.readLine(); 
     System.out.println("Text received: " + line); 
     } 


     c.close(); 
     is.close(); 
     os.close(); 


    } catch (UnknownHostException ex) { 
     System.err.println("Don't know about host"); 

    } catch (Exception e) { 
     System.err.println("IOException: " + e); 

    } 

} 


} 

編輯:我發現這個問題,這是因爲書面方式MainClient.runAClient的( )在代碼中,我應該把它放在哪裏?請幫幫我。

+3

此代碼甚至沒有編譯。請將您發佈的示例代碼修改爲儘可能最小但仍可編譯的示例,以展示您遇到問題時的行爲。此外,請查閱http://sscce.org/以獲取有關如何發佈正確代碼示例的進一步建議。 – Bombe 2010-01-13 13:21:15

+0

我發現了這個問題,這是因爲在這段代碼中添加了MainClient.runAClient()。我應該把它放在哪裏? – Johanna 2010-01-13 17:57:53

回答

1

這個article包含一個sscce,說明一個簡單的客戶端服務器GUI。您可能會發現它很有啓發性。如果是這樣,請考慮如何解決Echo(Kind kind)構造函數最後一行中發現的錯誤。

+0

我發現了這個問題,這是因爲在代碼中編寫了MainClient.runAClient()方法。請問我應該在哪裏放這行?謝謝 – Johanna 2010-01-13 18:07:36

+0

我沒有辦法測試你的代碼。通常,您可以在服務器準備好接受新連接後的任何時候啓動客戶端線程。在我引用的例子的'main()'方法中,服務器首先啓動,然後是客戶端;兩者都運行直到關閉。 – trashgod 2010-01-13 19:19:58