2015-10-11 40 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

相关问题