我有两个java类。一个是我编写的GUI,另一个是我实现了接口(interface)的类(称为类2)。我的项目从GUI的主要方法开始。我想发送一个字符串从类 2 到我的 GUI 类,以便在文本区域中显示它,但什么也没有发生。
因为我的主要 gui 类是
public class GraphicalInterface extends javax.swing.JFrame{
//I have created a function over here for displaying string in text area
public void show1(String name)
{
jTextArea1.setText(name);
}
//buttons code
public static void main(String args[]) {
//code
}
}
我在我的类 2 中创建了该类的一个对象,如下所示
GraphicalInterface b=new GraphicalInterface();
b.show1("pear");// it does not allow me to write this statement
请帮我解决如何从另一个java类调用main方法类。谢谢。
最佳答案
您可能尝试在构造函数或方法(或初始化 block )之外调用此代码,而在 Java 中,这是无法完成的。而是在方法或构造函数内调用此代码。
关于java - 从另一个java类调用main方法类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9288033/