2016-06-10 44 views
0

我有一個帶有JMenuBar的程序。 JMenuBar內部有一個JButton,JButton打開了一個JMenu,它有兩個JMenuItems。當您點擊其中一個項目時,它會打開第二個JFrame,並逐行打印文本文件的內容。Java - JPanel - 用arraylist繪製文本文件的內容 - 僅在第一次運行

如果您選擇其他選項(在同一個會話中),它應該更改JFrame標題(這起作用),並打印其他文本文件的內容(這不起作用)。

我已經指出了問題所在,但我不知道爲什麼會發生此問題。第一次顯示文本文件的內容時,代碼完美無缺。但是,第二次不改變文字。

我在第二次發現的內容在評論中描述。從setDocument方法開始,然後移動到paintComponent方法。

這裏是問題(還有其他一些類節目,但問題僅僅是這個類中)的類...

package PeriodicTable; 

import javax.swing.JPanel; 

import java.util.ArrayList; 

import java.io.IOException; 
import java.io.FileReader; 
import java.io.BufferedReader; 

import java.awt.Graphics; 
import java.awt.Color; 

import java.lang.Override; 

class DocumentPanel extends JPanel { 
private ArrayList<String> aryDocument; 
DocumentPanel(){ 
    super(); 
    setBackground(Color.white); 
} 
@Override 
protected void paintComponent(Graphics gr){ 
    super.paintComponent(gr); 
    //aryDocument holds the contents of the old text file (should have the contents of the other text file) 
    for(int index = 0; index < aryDocument.size(); index++){ 
     //enters loop, re-prints the old document 
     gr.drawString(aryDocument.get(index), 5, (index + 1)*10); 
    } 
} 
public void setDocument(String strFileDirectory){ //places contents of text file in an array (line by line) 

    //aryDocument is null at this time 
    aryDocument = new ArrayList<String>(); 
    //aryDocument is empty at this time 
    try(BufferedReader reader = new BufferedReader(new FileReader(strFileDirectory))){ 
     for(String strLine; (strLine = reader.readLine()) != null;){ 
      aryDocument.add(strLine); 
     } 
     reader.close(); 
    }catch(IOException ioe){ 
     ioe.printStackTrace(); 
    } 
    //aryDocument holds the contents of the other text file 
    this.revalidate(); 
} 
} 

下面的方法是在一個類中調用表(表實現了ActionListener)。這個方法由actionPerformed調用,他使用ActionCommand值來確定什麼動作做什麼。

private void loadTextFile(String strName, String strFileDirectory){ 
    DocumentPanel clsDocumentPanel = new DocumentPanel(); 
    if(frmDocument == null){ 
     frmDocument = new JFrame(strName); 
     frmDocument.setPreferredSize(new Dimension(600, 700)); 
     clsDocumentPanel.setDocument(strFileDirectory); 
     frmDocument.add(clsDocumentPanel); 
     frmDocument.pack(); 
     frmDocument.setVisible(true); 
    }else{ 
     if(!(frmDocument.getTitle().equals(strName))){ 
      frmDocument.setTitle(strName); 
      clsDocumentPanel.setDocument(strFileDirectory); 
      frmDocument.pack(); 
      frmDocument.setVisible(true); 
     } 
    } 
} 

我重新檢查了strFileDirectory並確認它們是正確的值。

我正在使用哪些版本/程序/ etc?

的Java 8

記事本

命令提示符

我在明確規定的問題...

爲什麼不aryDocument的價值,當它進入的paintComponent(加載後,其他變化文本文件)?我該如何解決它?

+0

誰(線程)調用'setDocument'? –

+0

默認線程系統,我沒有做任何使用此程序的線程。至於導致setDocument的路徑,當您單擊JMenuItem時,actionPerformed方法會對ActionCommands進行排序以確定要執行的操作。這是在不同的課上完成的。 – Tyler

+0

等等,你是說你調用'setDocument',然後*調用paintComponent,它的舊值是'setDocument',一切都在單個線程中? –

回答

3

這裏,

DocumentPanel clsDocumentPanel = new DocumentPanel(); // <- yes, here! 
if(frmDocument == null){ 
    frmDocument = new JFrame(strName); 
    frmDocument.setPreferredSize(new Dimension(600, 700)); 
    clsDocumentPanel.setDocument(strFileDirectory); 
    frmDocument.add(clsDocumentPanel); 
    frmDocument.pack(); 
    frmDocument.setVisible(true); 
}else{ 
    if(!(frmDocument.getTitle().equals(strName))){ 
     frmDocument.setTitle(strName); 
     clsDocumentPanel.setDocument(strFileDirectory); 
     frmDocument.pack(); 
     frmDocument.setVisible(true); 
    } 
} 

您創建的執行動作每次DocumentPanel一個新的實例。您第一次將其添加到frmDocument。你第二次打電話給setDocument,讓它被垃圾收集。第一個實例(實際上連接到顯示的幀)從不更新。

所以,要麼

  • clsDocumentPanel分開的地方,就像您存儲frmDocument作爲一個局部變量;
  • 使frmDocument一個JFrame子類公開訪問其文檔面板和調用frmDocument.getDocumentPanel().setDocument(...) - 但這違反了德米特定律;
  • 或使frmDocument a JFrame子類有一個setDocument方法,只是委託其面板setDocument,然後只需撥打frmDocument.setDocument(...)
+0

我將DocumentPanel引用創建移動到方法的外部,並將實例創建到Table的構造函數中,但問題仍然存在。我將繼續在OP中獲取此System.outs和命令結果。 – Tyler

+0

取消最後的評論,我之前做過的修改干擾了修復。撤消該編輯後,它現在工作正常。謝謝您的幫助! – Tyler

相關問題