我有这个问题:
我有一个 JFrame GUI 类 GUIView.java :
public class GUIView extends JFrame{
//other instance variables
....
public JTextArea gameGuide;
//constructor
public GUIView(){
...
initGUI();
}
//init GUI
public void initGUI(){
//add other components
...
gameGuide = new JTextArea();
//set layout
...
add(gameGuide);
}
public void setText(String s){
this.gameGuide.setText(s);
}
public void showGame(){
GUIView f = new GUIView() ;
f.setSize(450,600);
f.setTitle("TIC TAC TOE ONLINE");
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
-然后我有一个名为 Worker.java 的文件
public class Worker {
public GUIView guiView;
public Worker(GUIView guiView) {
this.guiView= guiView;
}
public static void main(String[] args){
GUIView guiView = new GUIView();
Worker worker = new Worker(guiView);
guiView.setText("testing if this will work");
guiView.showGame();
} }
GUI 工作正常,它显示一个空白的 gameGuide 文本区域。
在 Worker.java 中,我尝试更改 gameGuide 的内容,但是 gameGUI.setText() 中的字符串没有显示。
我试过设置可见真->假->真,仍然没有用。
请解释为什么?有没有一种方法可以更改 Worker.java 中组件 gameGuide textArea 的内容?
这可能会有所帮助。实际上,创建了两个不同的 GUIView JFrame。
public void showGame(){
setSize(450,600);
setTitle("TIC TAC TOE ONLINE");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句