我猜想你只是想改变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();
}
});
}
}
如果没有更多的代码,它可能很难引导你,但你能不能因此持有它需要的参考textPane的情况下进入SomePrintClass的构造? – Michael
@Michael这听起来很合理,你能否给我举一个你如何做的例子? – Aven
是否要对其他类中的JEditorPane进行更改? – CodeRunner