2012-04-18 18 views
1

私はゲーム用のGUIを作ろうとしています。まず、jFrameにウェルカム画面を読み込みます。彼らは詳細を入力し、詳細が正しい場合は別のクラスにある次の画面を読み込む必要があります。それは、詳細を正確にチェックし、新しいウィンドウを開きますが、それを表示しない、それはちょうど黒ですか?私はそれが私のコンピュータか自分のコードかどうか分からないのですか?Netbeansの新しいウィンドウは完全に黒で黒ですか?

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

public class ClientGUI extends javax.swing.JFrame { 

/** 
* Creates new form ClientGUI 
*/ 
private BufferedWriter writer; 
public ClientGUI() { 
    /* 
    * Set the Nimbus look and feel 
    */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* 
    * If Nimbus (introduced in Java SE 6) is not available, stay with the 
    * default look and feel. For details see 
    * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* 
    * Create and display the form 
    */ 
    setVisible(true); 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialise 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"> 

    jButton1.setText("Move N"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jButton2.setText("Move E"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 

    jButton3.setText("Move S"); 
    jButton3.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton3ActionPerformed(evt); 
     } 
    }); 

    jButton4.setText("Move W"); 
    jButton4.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton4ActionPerformed(evt); 
     } 
    }); 

    jButton5.setText("Pickup"); 
    jButton5.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton5ActionPerformed(evt); 
     } 
    }); 

    jButton6.setText("Quit"); 
    jButton6.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton6ActionPerformed(evt); 
     } 
    }); 



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

    try{ 
     writer.write("Move N"); 
     writer.newLine(); 
     writer.flush(); 
    } 
    catch(Exception e){ 
      System.out.println("error"); 
    } 

}           

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
    try{ 
     writer.write("Move E"); 
     writer.newLine(); 
     writer.flush(); 
    } 
    catch(Exception e){ 
      System.out.println("error"); 
    } 
}           

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
    try{ 
     writer.write("Move S"); 
     writer.newLine(); 
     writer.flush(); 
    } 
    catch(Exception e){ 
      System.out.println("error"); 
    } 
}           

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {           
    try{ 
     writer.write("Move W"); 
     writer.newLine(); 
     writer.flush(); 
    } 
    catch(Exception e){ 
      System.out.println("error"); 
    } 
}           

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {           
    try{ 
     writer.write("PICKUP"); 
     writer.newLine(); 
     writer.flush(); 
    } 
    catch(Exception e){ 
      System.out.println("error"); 
    } 
}           

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {           
    setVisible(false); 
}           

/** 
* @param args the command line arguments 
*/ 
public void run(){ 
try { 
     final Socket server = new Socket("localhost", 30000); 
    //Creates the socket to be used, has to be final because of innerclass 
    InputStream in = server.getInputStream(); 
    OutputStream out = server.getOutputStream(); 
    //Creates the input and output streams 

    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 
    //Creates the reader to read what the client inputs 
    final BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
    //Creates the reader to read what the server writes to the client, has to be final as used in innerclass 
    writer = new BufferedWriter(new OutputStreamWriter(out)); 
    //Creates the writer so the client can write on the commandline 

    Thread serverThread = new Thread(){ 
     //Modifies the Thread class 
     public void run(){ 
     boolean quit = false; 
     while (!quit){ 
      String line = ""; 
      try{ 
      line = reader.readLine(); 
      //Tries to read what the server has sent 
      } 
      catch(IOException e){ 
      System.out.println("Error" + e.getMessage()); 
      quit = true; 
      //If it is unable to read what the server sent, it quits the loop 
      } 
      if (line != "null"){ 
       System.out.println(line); 
      //So long as the line sent is not "null" it prints the line to the client 
      } 
     } 
     }   
    }; 
    serverThread.start(); 
    //Starts the Thread 

    String input = stdin.readLine(); 
    while (input != null) { 
    writer.write(input); 
    writer.newLine(); 
    writer.flush(); 
    input = stdin.readLine(); 
    //Reads what the client writes 
    } 
    System.exit(1); 
    server.close(); 
} 
catch (IOException e) { 
    System.out.println("error" + e.getMessage()); 
    e.printStackTrace(); 
    //Catches any errors with reading and writing to and from the server 
} 
} 

プラスNetBeansは、デフォルトではないいくつかの余分なスイングビット: これにより、第2のウィンドウのためのコードです。 ご協力いただきありがとうございます。

+0

そのコードには73のコンパイルエラーがあります。もっと早く助けを求めるには、[SSCCE](http://sscce.org/)を投稿してください。 –

+0

* "これは2番目のウィンドウのコードです:" * [複数のJFramesの使用、良い/悪い練習ですか?](http://stackoverflow.com/a/9554657/418556) –

答えて

2

あなたの問題はここにあり:

setVisible(true); 
initComponents(); 

あなたはこの順序を逆にする必要があります

initComponents(); 
setVisible(true); 

はまた、あなたは、ソケットの仕事をやっているように見えます。もしそうなら、うまくいけば、主なSwingイベントスレッドであるEDTではなく、バックグラウンドスレッドでこれを行うように気をつけてください。

+0

これを試しましたが、新しいウィンドウは完全に空白で、もう黒ではありません。しかし、ありがとう – Coolmurr

+0

@Coolmurr:あなたはまた、スレッドの2番目の問題に対処できますか? –

+0

私はこれが問題ではないと思っています。以前にスレッドを使って作業していたので、 – Coolmurr

関連する問題