是否可以从初始化的 class 外部更改对象实例?
is it possible to change an object instance from outside the class it was initialized in?
我可能以错误的方式处理这件事。请赐教。提前致谢!!!
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.....
这是一个简单的例子,基本上你通过构造函数将一个 class 的实例传递给另一个。也可以通过其他方式完成...
public class Whosebug_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)
{
Whosebug_33061019 ap=new Whosebug_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());
}
}
我猜想您只想更改 JEditorPane 的文本,而不是其他 class。
如果是这样,那就很简单了。 Make the JEditorPane static
并使用 class 的名称调用其 setText() 方法。例如。
第一个Class.
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);
}
}
第二个Class.
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();
}
});
}
}
我可能以错误的方式处理这件事。请赐教。提前致谢!!!
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.....
这是一个简单的例子,基本上你通过构造函数将一个 class 的实例传递给另一个。也可以通过其他方式完成...
public class Whosebug_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)
{
Whosebug_33061019 ap=new Whosebug_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());
}
}
我猜想您只想更改 JEditorPane 的文本,而不是其他 class。
如果是这样,那就很简单了。 Make the JEditorPane static
并使用 class 的名称调用其 setText() 方法。例如。
第一个Class.
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);
}
}
第二个Class.
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();
}
});
}
}