2013-05-07 30 views
0

當在JEditorPane中設置的子類JFrame中的新文本時,我遇到問題。在JEditorPane中設置新文本

package gui; 
... 
public class Index extends JFrame { 
    JEditorPane editorPaneMR = new JEditorPane(); 

public static void main(String[] args) { 
    ... 
} 

public Index() { 
     JButton SearchButton = new JButton("OK"); 
     SearchButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       parser GooBlog = new parser(url); 
       try { 
        GooBlog.hello(); // Go to subclass parser 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

而這個所謂的解析器

子類的代碼
package gui; 
public class parser extends Index{ 
    String url; 

    public parser (String urlInput){ 
     this.url = urlInput; 
    } 

    public void hello() throws IOException{ 
     editorPaneMR.setText("Hello World"); 
    } 
} 

問題是,當我按下OK按鈕它不顯示我的JEditorPane文本「世界,你好!」並沒有顯示任何錯誤,只是沒有發生任何事情。

+2

檢查內容類型(或的EditorKit)。如果它是例如HTML提供正確的HTML,但不是純文本。 – StanislavL 2013-05-07 09:49:19

+2

參見[*編輯窗格與文本窗格*](http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html#recap)。 – trashgod 2013-05-07 11:31:55

+0

我不認爲圍繞編輯器類型的pbm我認爲它是圍繞着如何在子類中的editotpaneMR字段工作! – 2013-05-07 14:06:02

回答

1

的代碼行

parser GooBlog = new parser(url); 

實例化不僅是一個解析器也是一個新的Index/JFrame。這個新創建的JFrameJEditorPane用於內部方法hello而且由於框架是不可見的,什麼都不會發生。

一種解決方案可能是提供對您的JFrameJEditorPane的引用,例如將方法hello,例如,

public class Parser { // does no longer extend Index 
    String url; 

    public Parser(String urlInput) { 
     this.url = urlInput; 
    } 

    public void hello(JEditorPane editorPane) { // has argument now 
     editorPane.setText("Hello World"); 
    } 
} 

這將隨後通過

Parser gooBlog = new Parser(url); 
gooBlog.hello(Index.this.editorPaneMR); 

注被稱爲:請堅持常見的Java編碼標準和使用類的大寫名稱的即Parser代替parser,和較低的情況下,可變/字段/方法名稱,例如gooBlog。使用有

+0

謝謝兄弟你幫了我很多 – 2013-05-07 20:01:44