2012-04-21 59 views
1

我想在netbeans中使用swing執行一個簡單的客戶端服務器通信。首次運行時,它顯示一個空白窗口。我再次運行它而不關閉第一個輸出窗口,它顯示所需的輸出。我附上代碼。擺動輸出沒有在第一次運行

感謝

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

public class serverg extends javax.swing.JFrame { 

ServerSocket ss; 
Socket s; 
BufferedReader in; 
PrintWriter out; 

public serverg() { 
    initComponents();  
} 

void work() { 
    try { 

     String line; 
     ss = new ServerSocket(3500); 
     s=new Socket(); 
     jTextArea1.append("waiting for connection"); 
     s=ss.accept(); 
     in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     out = new PrintWriter(s.getOutputStream()); 
     out.print("connection established"); 
     if((line=in.readLine())!=null) 
     jTextArea1.append("Client : " + line); 

    } catch (IOException r) { 
    } 

} 

@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jScrollPane1 = new javax.swing.JScrollPane(); 
    jTextArea1 = new javax.swing.JTextArea(); 
    jTextField1 = new javax.swing.JTextField(); 
    jButton1 = new javax.swing.JButton(); 
    jLabel1 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setName("Form"); // NOI18N 

    jScrollPane1.setName("jScrollPane1"); // NOI18N 

    /*try{ 
     jTextArea1.setEditable(false); 
     //s=new Socket(); 
     jTextArea1.append("waiting for client..."); 
     s=ss.accept(); 
     String line=in.readLine(); 
     jTextArea1.append("Client : " + line); 
    } 
    catch(IOException r){}*/ 
    jTextArea1.setColumns(20); 
    jTextArea1.setRows(5); 
    jTextArea1.setName("jTextArea1"); // NOI18N 
    jScrollPane1.setViewportView(jTextArea1); 

    jTextField1.setName("jTextField1"); // NOI18N 

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getResourceMap(serverg.class); 
    jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N 
    jButton1.setName("jButton1"); // NOI18N 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jLabel1.setName("jLabel1"); // NOI18N 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(10, 10, 10) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(98, 98, 98) 
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap()) 
    ); 

    pack(); 
}// </editor-fold>       

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    String line = jTextField1.getText(); 
    out.println(line); 
    jTextArea1.append("\nServer : " + line); 
}           

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 

    java.awt.EventQueue.invokeLater(new Runnable() { 

     public void run() { 
      serverg ob=new serverg();     
      ob.setVisible(true);      
      ob. jTextArea1.append("waiting for connection" ); 
      ob.work();    
     } 
    }); 

} 
// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JScrollPane jScrollPane1; 
private static javax.swing.JTextArea jTextArea1; 
private javax.swing.JTextField jTextField1; 
// End of variables declaration     
} 

回答

3

work()方法是不是你的Swing Application的一部分,因此,您main()方法是錯誤的,只是改變你的work()方法這一點,所以,無論是過程可以在各自的線程運行,通過編寫s=ss.accept();,您屏蔽了您的事件派發器線程。

此外,您想要對GUI進行的更改必須在事件分派器線程上完成,因此您需要更改work()方法,請參閱Concurrency in Swing以獲取更多信息。因此,以這種方式恢復您的主要方法,以解決問題。

public static void main(String args[]) 
{ 
    final serverg ob=new serverg(); 
    java.awt.EventQueue.invokeLater(new Runnable() 
    { 
     public void run() 
     {       
      ob.setVisible(true);      
      ob. jTextArea1.append("waiting for connection" );       
     }  
    }); 
    ob.work(); 
} 

你好像裏面work()方法塊,必須是這樣的,對於這個工作,你必須聲明你line變量,實例變量:

if((line=in.readLine())!=null) 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      jTextArea1.append("Client : " + line); 
     } 
    }); 
相關問題