我有两个类Game
和wampusGUI
类。在wampusGUI
类中,我在textarea
方法下有一个名为displayTextArea
的textarea1()
。
我正在尝试将append
结果从textarea
类转换为Game
。但是当我试图从那个 class 访问时。 function
运行正常,结果也进入了该类(我只是通过System.out.print()
方法进行了测试),但未将其附加到textarea
。这是我的代码。
// Code of wampusGUI class
public class wampusGUI extends javax.swing.JFrame {
/**
* Creates new form wampusGUI
*/
public wampusGUI() {
initComponents();
}
public void textArea1(String text) {
System.out.print(text);
displayTextArea.append(text); // this is not appending to textarea.
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new wampusGUI().setVisible(true);
Game g = new Game();
g.testing();
}
});
}
//这里是Game类的代码
private wampusGUI gui;
public void testing () {
String welCome=welcome();
gui= new wampusGUI();
gui.textArea1(welCome);
}
最佳答案
在您的代码中进行更改
在您的头等舱wampusGUI
public class wampusGUI extends javax.swing.JFrame {
/**
* Creates new form wampusGUI
*/
public wampusGUI() {
initComponents();
}
public void textArea1(String text) {
System.out.print(text);
displayTextArea.append(text); // this is not appending to textarea.
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
wampusGUI w=new wampusGUI();
w.setVisible(true);
Game g = new Game(w);
g.testing();
}
});
}
对于二等游戏
private wampusGUI gui;
//Add Contructor with Parameter
public Game(wampusGUI w){
//put this line of code at the end
gui=w;
}
public void testing () {
String welCome=welcome();
gui.textArea1(welCome);
}
这将工作...