2015-10-11 42 views
0

我可能會以錯誤的方式討論這件事。請賜教。謝謝提前!是否可以從初始化類的外部更改對象實例?

void initialize() { 
more code... 
JEditorPane textPane = new JEditorPane(); 
textPane.setEditable(false); 
textPane.setBackground(Color.LIGHT_GRAY); 
textPane.setText(" THE MESSAGE I WANT TO CHANGE FROM OUTSIDE initialize()"); 
more code.... 

public static void SomePrintClass(){ 
JEditorPane textPane = new JEditorPane(); 
textPane.setText("SOME NEW TEXT);  // I am aware this doesn't work 
//but is there a way it can be made to work??? 
more code..... 
+1

如果沒有更多的代碼,它可能很難引導你,但你能不能因此持有它需要的參考textPane的情況下進入SomePrintClass的構造? – Michael

+0

@Michael這聽起來很合理,你能否給我舉一個你如何做的例子? – Aven

+0

是否要對其他類中的JEditorPane進行更改? – CodeRunner

回答

2

我猜想你只是想改變JEditorPane的文本從任何其他類。 如果是這樣,那很簡單。 Make the JEditorPane static並用該類的名稱調用其setText()方法。例如。

頭等艙

public class First extends JFrame { 

    static JEditorPane ep; 
    First() { 
     ep = new JEditorPane(); 
     setSize(new Dimension(200, 200)); 
     ep.setText("I expect to receive some text."); 
     add(ep); 
     setVisible(true); 
    } 

    @Override 
    public void paintComponents(Graphics g) { 
     super.paintComponents(g); 
    } 
} 

第二類。

public class Second extends JFrame { 

    JButton btn; 
    JTextField jtf = new JTextField(16); 
    JEditorPane ep; 

    Second() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     btn = new JButton("Send above Text."); 
     setSize(new Dimension(200, 200)); 
     btn.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       ep = First.ep; 
       ep.setText(jtf.getText()); 
       ep.setForeground(Color.red); 
      } 

     }); 
     this.setLayout(new FlowLayout()); 
     add(jtf); 
     add(btn); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      First so; 

      @Override 
      public void run() { 
       new Second(); 
       so = new First(); 
      } 
     }); 
    } 
} 
+0

感謝兄弟這是我最終實現的,它的工作原理是我最初設想的!支持你成爲一名心靈讀者! – Aven

+0

我的快樂。兄弟非常感謝。我們是來幫你的。要開心。 – CodeRunner

1

這裏是一個簡單的例子,基本上你通過構造函數將一個類的實例傳遞給另一個類。它也可以通過其他方式完成......

public class StackOverflow_33061019 { 

    public class ExampleClass 
    { 
     String displayText; 

     public ExampleClass() 
     { 
     } 

     public String getDisplayText() 
     { 
      return displayText; 
     } 

     public void setDisplayText(String text) 
     { 
      this.displayText = text; 
     } 
    } 

    public class AnotherClass 
    { 
     ExampleClass updateMe; 

     public AnotherClass(ExampleClass example) 
     { 
      updateMe = example; 
     } 

     public void changeText() 
     { 
      updateMe.setDisplayText("Updated text from AnotherClass"); 
     } 
    } 

    public static void main(String[] args) 
    { 
     StackOverflow_33061019 ap=new StackOverflow_33061019(); 
     ap.runIt(); 
    } 

    public void runIt() 
    { 
     ExampleClass example = new ExampleClass(); 
     example.setDisplayText("Initial text"); 
     System.out.println("ExampleClass displayText: " + example.getDisplayText()); 

     AnotherClass another = new AnotherClass(example); 
     another.changeText(); 
     System.out.println("ExampleClass displayText: " + example.getDisplayText()); 
    } 

} 
+0

這可能會起作用,但我必須回頭重新思考我的原始思路。謝謝你的這個例子。 – Aven

+0

他們不必是內部類,也有其他方法來做到這一點。這只是通過引用傳遞實例並保存它的示例。您可以通過其他方式共享對象的訪問權限,或者您可以儘可能使事件總線/偵聽器根據事件進行更新。不知道更多關於你在做什麼,這些只是一般性建議 – Michael

相關問題