是否可以从初始化的类外部更改对象实例?

时间:2015-10-11 02:41:18

标签: java swing class instance

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

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

2 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

答案 1 :(得分: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());
    }

}