2012-01-12 115 views
0

喜的朋友,我在的Java Swing創建一個網吧同治軟件,我想從server.i退出客戶機上的Java軟件(在擺動歡迎窗口)的Java Swing有這樣的代碼BT它不是working.when客戶端運行的程序窗口擺不visible.i我能夠從服務器關閉它,但我想要的是當客戶端編譯,當我從服務器大火收命令運行的代碼,收盤有可能客戶端

鞦韆窗口應該是可見的
import java.net.*; 
import java.io.*; 

public class cl extends javax.swing.JFrame { 

    /** Creates new form cl */ 
    public cl() { 


    initComponents(); 
} 

/** This method is called from within the constructor to 
* initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is 
* always regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 
    jLabel5 = new javax.swing.JLabel(); 
    jLabel1 = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setBackground(new java.awt.Color(255, 204, 204)); 

    jPanel1.setBackground(new java.awt.Color(255, 204, 204)); 

    jLabel5.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\new-1.jpg")); // NOI18N 

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N 
    jLabel1.setText("Welcome to our cafe"); 

    jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N 
    jLabel2.setText("Contact Administrator to start your session"); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jLabel5) 
      .addGroup(jPanel1Layout.createParallelGroup 
    (javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(92, 92, 92) 
        .addComponent(jLabel1)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(42, 42, 42) 
        .addComponent(jLabel2))) 
      .addContainerGap(872, Short.MAX_VALUE)) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGroup(jPanel1Layout.createParallelGroup 
(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addContainerGap() 
        .addComponent(jLabel5)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(32, 32, 32) 
        .addComponent(jLabel1) 
        .addGap(18, 18, 18) 
        .addComponent(jLabel2))) 
      .addContainerGap(597, Short.MAX_VALUE)) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 
     javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 
     javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)  


    ); 

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

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


try 
    { 

    String s1,s2; 
    Socket s=new Socket("192.168.1.2",1024); 
        DataInputStream dis=new DataInputStream(s.getInputStream()); 
    DataOutputStream dos=new DataOutputStream(s.getOutputStream()); 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 

    while(true) 
    { 



s1=dis.readUTF(); 

if (s1.equals("5")) 
{ 
System.exit(0); 
} 
    } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
// Variables declaration - do not modify 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JLabel jLabel5; 
private javax.swing.JPanel jPanel1; 
// End of variables declaration 

}

+2

請學習Java命名約定並嚴格遵守 – kleopatra 2012-01-12 07:13:44

回答

2

問題是,您從不在main()方法中創建幀。

將此代碼添加到您的主要方法:

public static void main(String args[]) { 
    cl frame = new cl(); 
    cl.setSize(640,480); 
    cl.setVisible(true); 
    // rest of code follows... 

然後框便會出現。

是否希望用戶能夠關閉框架您還可能要進行調查,並JFrame.setDefaultCloseOperation(int)

我也建議你考慮使用java RMI,而不是自己的協議。 RMI可能會給你更多的功率/功能,而你需要被手動編碼聯網。 RMI也將意味着你不會需要運行在一個單獨的線程監聽:RMI將處理爲你。

+0

感謝@craigmj ..它的工作。 – 2012-01-13 02:47:44

1

可能不是一個回答您的問題,請

1)不使用生成的代碼從NetBeans中,使用Standard Swing JComponents

2)尋找正確LayoutManager,因爲很難管理由GroupLayout

3)重定向Socket到後臺任務生成的代碼串,使用SwingWorker,Runnable#Thread,否則GUI將凍結,直到長時間持續結束

+0

(壞笑)..但我看你是充滿可能的。沒關係。 ;) – 2012-01-12 09:15:30