2014-01-05 65 views
1

我借用了我在stackoverflow上找到的設計將控制檯輸出重定向到GUI。它工作正常,直到我開始閱讀我的程序中的文本文件。現在,當我使用GUI運行程序時,不顯示任何輸出,GUI將凍結,然後最終自行關閉。這裏是我的GUI代碼削減版本:將控制檯輸出重定向到GUI

public class GreenhouseControlsGUI { 
    public static class MyGui extends JFrame implements ActionListener { 

     // Start button 
     JButton Start = new JButton("Start"); 

     /..................................../ 

     /** 
     * The constructor. 
     */ 
    public MyGui(){  
     super("Greenhouse Controls"); 

      /............................../ 

     bottomPanel.setLayout(new FlowLayout()); 
     bottomPanel.add(Start); 

      /............................../ 

     getContentPane().add(holdAll, BorderLayout.CENTER); 

     Start.addActionListener(this); 

      /............................../ 

     setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    } 

    public void actionPerformed(ActionEvent e){ 

     if (e.getSource() == Start) 
      GreenhouseControls.startMeUp(); // Start program... 

      /............................../ 
     } 

    public static void main(String[] args){ 

     MyGui myApplication = new MyGui(); 

     // redirect output to GUI 
     myApplication.redirectSystemStreams(); 

     // Specify where will it appear on the screen: 
     myApplication.setLocation(10, 10); 
     myApplication.setSize(500, 300); 

     // Show it! 
     myApplication.setVisible(true); 
     } 

     private void updateTextArea(final String text) { 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
       myText.append(text); 
       } 
      }); 
      } 

     private void redirectSystemStreams() { 

      OutputStream out = new OutputStream() { 

       @Override 
       public void write(int b) throws IOException { 
       updateTextArea(String.valueOf((char) b)); 
       } 

       @Override 
       public void write(byte[] b, int off, int len) throws IOException { 
       updateTextArea(new String(b, off, len)); 
       } 

       @Override 
       public void write(byte[] b) throws IOException { 
       write(b, 0, b.length); 
       } 
      }; 

      System.setOut(new PrintStream(out, true)); 
      System.setErr(new PrintStream(out, true)); 
      } 
    } 
} 

我敢肯定我的問題,從代碼中的「文件名」路徑讀取開始後面,因爲我的時候我發表意見沒有這個問題'filename'變量聲明出來。我認爲將控制檯輸出重定向到我的GUI的方法只是重定向輸出....爲什麼當我從文件中讀取時會把所有東西搞砸?我是編程新手,我可能忽視了一些明顯的東西,但我無法弄清楚。

這裏是GUI類中調用靜態startMeUp()方法:

public static void startMeUp() { 
     try { 
      String option = "-f";     // (-f) to start program or (-d) to restore program 
      filename = "src/greenhouse/examples3.txt"; // read file from here 
      dumpFile = "dump.out";      // restore program from here 

      // if option is invalid invoke corresponding print statement 
      if (!(option.equals("-f")) && !(option.equals("-d"))) { 
       System.out.println("Invalid option"); 
       printUsage(); 
      } 

      GreenhouseControls gc = new GreenhouseControls(); // Create GreenhouseControls object 
      Restart restart = new Restart(0,filename, gc); // Create Restart object 
      Restore restore = new Restore(0,dumpFile);  // Create Restore object 

      // if the option value is -f ... 
      if (option.equals("-f")) { 
       gc.addEvent(restart);  // add new Restart object to addEvent() 
      } 
      gc.run();         

      // if the option value is -d ... 
      if (option.equals("-d")) { 
       gc.addEvent(restore);  // add new Restore object to addEvent() 
      } 
      gc.run(); 

      }catch (ArrayIndexOutOfBoundsException e) { 
      System.out.println("Invalid number of parameters"); 
      printUsage(); 
     } 
    } 
+0

我的善良什麼可怕的想法,重定向標準錯誤。當然這會使調試這個問題變得困難。註釋掉'System.setErr'行(新的PrintStream(out,true));'並重試。你有沒有在控制檯/終端窗口看到任何東西? –

+0

試過你的建議,我仍然有同樣的問題。沒有輸出到GUI – LooMeenin

+0

在*控制檯*或*終端窗口*上怎麼樣?我不是在談論GUI。你有一個控制檯或終端窗口打開你不? –

回答

4

你需要在一個新的線程調用startMeUp,因爲你的控制檯程序阻止事件調度線程。就像這樣:

new Thread() { 
    @Override public void run() { 
    GreenhouseControls.startMeUp(); 
    } 
}.start(); 

,而不是僅僅

GreenhouseControls.startMeUp(); 
+2

個人而言,我會推薦一個SwingWorker over一個線程,因爲它具有幫助重新同步EDT更新的功能 – MadProgrammer

相關問題