2012-10-20 16 views
0

我有一個GUIPanel已經創建好了,我需要將它添加到我有的程序中,當我嘗試從主類中運行另一個程序時,GUIPanel不會運行,但是當我不執行它時(如果這是有意義的)。如何添加一個JFrame到我的程序

This is what happens when I run the program 這是當我運行該程序

This is the GUIPanel I want the output to print to

我想要的輸出打印到這個GUIPanel(我不得不刪除調用TourServer得到這個運行)會發生什麼,如果你在想:「爲什麼不把它留在命令提示符中,除了文本區域之外,你的GUIPanel中沒有任何東西?」,那麼這是因爲我打算在後面添加更多內容:D

總結:我需要從GUIPanel類運行TourServer程序,當我嘗試它不打開GUI面板本身

這裏是主類:

import java.net.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.awt.*; 
import javax.swing.*; 
import java.io.*; 


/** 
* 
* @author Dan 
*/ 
public class GUIPanel extends JFrame { 
    private PrintStream outStream; 
    /** 
    * Creates new form GUIPanel 
    */ 
    public GUIPanel() { 
     initComponents(); 
    } 
    private void init() throws IOException { 
     jTabbedPane1.add("Main", jPanel); 
     jPanel.add(textArea1); 
     setOutputStream(); 
     TourServer.init(cmd);//Calling the program, I'm assuming I might be doing this wrong. 
    } 
    /** 
    * 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") 
    private void initComponents() { 

     textArea1 = new java.awt.TextArea(); 
     jPanel = new javax.swing.JPanel(); 
     jTabbedPane1 = new javax.swing.JTabbedPane(); 



     textArea1.setPreferredSize(new java.awt.Dimension(432, 300)); 



     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setTitle("Game Manager"); 
     setBounds(new java.awt.Rectangle(0, 0, 400, 450)); 
     setPreferredSize(new java.awt.Dimension(450, 420)); 
     getContentPane().setLayout(new java.awt.FlowLayout()); 

     try { 
      init(); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
     jTabbedPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
     jTabbedPane1.setFocusable(false); 
     jTabbedPane1.setOpaque(true); 
     getContentPane().add(jTabbedPane1); 
     jTabbedPane1.getAccessibleContext().setAccessibleName(""); 

     pack(); 
    } 




    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
    cmd = args; 
     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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 


     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new GUIPanel().setVisible(true); 
      } 
     }); 
    } 

    private static String[] cmd; 
    private javax.swing.JPanel jPanel; 
    private javax.swing.JTabbedPane jTabbedPane1; 
    private java.awt.TextArea textArea1; 

     //Written by MadProgrammer 
     public class CapturePane extends JPanel implements Consumer { 

     private TextArea output; 

     public CapturePane() { 
      jPanel.setLayout(new BorderLayout()); 
      jPanel.add(new JScrollPane(output)); 
     } 

     @Override 
     public void appendText(final String text) { 
      if (EventQueue.isDispatchThread()) { 
       output.append(text); 
       output.setCaretPosition(output.getText().length()); 
      } else { 

       EventQueue.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         appendText(text); 
        } 
       }); 

      }   
    } 
} 

    public interface Consumer {   
     public void appendText(String text);   
    } 

    public class StreamCapturer extends OutputStream { 

     private StringBuilder buffer; 

     public StreamCapturer() { 
      buffer = new StringBuilder(128); 
      buffer.append(""); 
     } 

     @Override 
     public void write(int b) throws IOException { 
      char c = (char) b; 
      String value = Character.toString(c); 
      buffer.append(value); 
      if (value.equals("\n")) { 
       textArea1.appendText(buffer.toString()); 
       buffer.delete(0, buffer.length()); 
      } 
     }   
    } 

} 

這裏是TourServer類

  // TourServer.java 
/* 
    The top-level Tour server, which waits for client connections 
    and creates TourServerHandler threads to handle them. 

    Details about each client are maintained in a TourGroup object 
    which is referenced by each thread. 

    Very similar to the multithreaded Chat server. 
*/ 

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


public class TourServer 
{ 
    static int PORT = 0; 

    private TourGroup tg; 
    static GameSession gameSession=new GameSession(); 
    public TourServer() 
    // wait for a client connection, spawn a thread, repeat 
    { 
    tg = new TourGroup(); 
    try { 
     ServerSocket serverSock = new ServerSocket(PORT); 
     Socket clientSock; 

     while (true) { 
     new GUIPanel("Waiting for a client..."); 
     clientSock = serverSock.accept(); 
     new TourServerHandler(clientSock, tg).start(); 
     } 
    } 
    catch(Exception e) 
    { e.printStackTrace(); } 
    } // end of TourServer() 


    // ----------------------------------- 

    public static void init(String args[]) { 
     if (Integer.parseInt(args[0]) > 0) { 
     PORT = Integer.parseInt(args[0]); 
     } else { 
      PORT = 5550; 
     } 
     new GUIPanel("Port set to: "+PORT); 
     new TourServer(); 
    } 
} // end of TourServer class 
+1

如果它沒有顯示面板,那是因爲它沒有在'JFrame'上調用'setVisible(true);'。但是你有太多的代碼讓別人去查找它應該去的地方。嘗試並將問題限制在相關部分。 –

+0

'javax.swing.UIManager.setLookAndFeel(..)'不要在代碼中設置基於完全不同原因的PLAF。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 (這是澳大利亞翻譯/解釋@Philip的清醒建議) –

回答

0

你有這樣的構造在GUI類:

public GUIPanel() { 
     initComponents(); 
    } 

在init函數後面添加這3個函數:

public GUIPanel() { 
     initComponents(); 
     validate(); 
     pack(); 
     show(); 
    } 

我確定後面會出現窗口。不要停止在這一點上,但試着改進這個解決方案,因爲至少有一個這些功能已被棄用,如果我記憶服務。對不起,我從Java 1.4開始就沒有真正使用過Swing。

+0

[修正:D](http://i50.tinypic.com/ja985g.png)謝謝@嚴格說,我這樣做了,除了什麼之外你提供的,我應該從TestServer類調用GUIPanel類,而不是像我那樣 – Dan

相關問題