【问题标题】:is it possible to change an object instance from outside the class it was initialized in?是否可以从初始化对象的类外部更改对象实例?
【发布时间】:2015-10-11 02:41:18
【问题描述】:

我可能以错误的方式处理这件事。请赐教。提前谢谢!!!

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.....

【问题讨论】:

  • 如果没有更多代码,可能很难指导您,但是您能否不将 textPane 的实例传递给 SomePrintClass 的构造函数,以便它持有所需的引用?
  • @Michael 这听起来很明智,你能给我举个例子说明你会怎么做吗?
  • 您想从任何其他类更改 JEditorPane 吗?
  • @CodeRunner 这可能会让我的生活更轻松。我正在尽一切可能找到最适合我正在构建的程序的方法。
  • 您可以通过我的示例对 JEditoPane 执行所有操作。我编辑了答案。更改了 JEditorPane 的字体颜色。

标签: java swing class instance


【解决方案1】:

我猜想您只想从任何其他类更改 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();
            }
        });
    }
}

【讨论】:

  • 谢谢兄弟,这是我最终实现的,它按照我最初的设想工作!支持你成为读心者!
  • 我的荣幸。大哥万分感谢。我们是来帮你的。开心点。
【解决方案2】:

这是一个简单的例子,基本上你通过构造函数将一个类的实例传递给另一个类。它也可以通过其他方式完成......

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());
    }

}

【讨论】:

  • 这可能行得通,但我必须回去重新考虑我最初的思路。谢谢你的例子。
  • 它们不必是内部类,还有其他方法可以做到。这只是通过引用传递实例并持有它的示例。您可以通过其他方式共享对对象的访问,或者您可以制作事件总线/侦听器以根据事件进行更新。不知道更多关于你在做什么,这些只是一般性的建议
猜你喜欢
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 2013-02-11
  • 2014-09-09
  • 2014-11-01
  • 1970-01-01
相关资源
最近更新 更多