2013-03-28 79 views
0

我正在簡單的文本編輯器和主面板上,我有JList與當前打開的文件(CDocument類)和活動文檔,顯示哪些內容(也是CDocument類)。我將打開的文件(CDocument對象)存儲在向量中,並在右側顯示活動文檔。 enter image description here文本複製

現在,在程序啓動時,沒有活動文檔,打開的文檔列表爲空。在我點擊File-> New之後,我從CDocument類創建了一個新的空對象。如果我在活動文檔區域(屏幕截圖上的紅色區域)輸入內容,然後重新點擊文件 - >新建,則會得到新的空文件(沒有文本 - 我用CDocument對象進行了雙重檢查)。但是,來自上一個活動文檔的文本仍顯示爲新創建的(紅色區域 - 新空CD文件)。因爲我不知道爲什麼,所以我在這裏打破了我的大腦?!以下是文件 - >新建代碼chunck: `

if(e.getSource()==this.menuItemFileNew()) 
{ 
    CDocument currentDocument=new CDocument(); 

    if(this.panelMain().documentActive()!=null) 
    { 
     this.panelMain().remove(this.panelMain().documentActive()); 
    } 

    this.panelMain().openedDocuments().add(currentDocument); 
    this.panelMain().setDocumentActive(currentDocument); 

    this.panelMain().add(panelMain().documentActive().pane(), 
      BorderLayout.CENTER); 
    this.panelMain().documentActive().addKeyListener(this); 
    this.panelMain().documentActive().requestFocus(); 

    this.menuItemFileSave().setEnabled(true); 
    this.menuItemFileSaveAs().setEnabled(true); 
    this.menuItemFileClose().setEnabled(true); 
    this.menuItemFileCloseAll().setEnabled(true); 

    this.toolBarFileSwitcher().panelActiveDocumentInfo(). 
      panelFileSizeInfo().updatePanel(this.panelMain().documentActive().getText().length(), 
       false); 

    this.toolBarFileSwitcher().listOpenedFiles().model().addElement(currentDocument.filename()); 
    this.toolBarFileSwitcher().listOpenedFiles().setSelectedIndex(this.toolBarFileSwitcher().listOpenedFiles().model().size()-1); 
    this.toolBarFileSwitcher().setVisible(true); 
} 

`

爲什麼顯示的文字,我已經試過updateUI,重新繪製,沒有任何作品!

+2

一個預測:我們將無法根據上面的代碼片段梳理出您的程序正在發生的事情,相反,我們需要您的問題的一個小的可編譯和可運行的版本,[sscce](http ://sscce.org)。我希望我錯了,但我不這麼認爲。 – 2013-03-28 01:53:44

+0

我可以在這裏發送一個jar文件,所以你可以運行它? – KernelPanic 2013-03-28 02:00:03

+0

我們是志願者,大部分人都忙於這類事情,包括我自己在內。此外,這將違背本網站的理念。 – 2013-03-28 02:02:54

回答

2

使用Action來封裝與您的CDocument數據類型相關的功能。這將有助於確保所有調用的一致性。這example管理圖像,而這example說明文件的菜單。

+1

+1超過100k!恭喜! – StanislavL 2013-03-28 06:51:02