当我在第二个jframe时,如何访问第一个jframe? [英] How do I access the first jframe while I'm in the second jframe?
问题描述
假设我运行第一个JFrame窗口,然后从第一帧开始打开第二帧。第一帧仍然打开,现在我想从第二帧访问第一帧,但如果我使用第一帧中的任何方法或对象,我会得到NullPointerException。即使我在网上看,但所有的视频都只讨论如何从第一帧访问第二帧。请帮帮我吧。
谢谢。
我尝试了什么:
第一帧:
//构造函数
class2 c = new class2();
//构造函数2
method1();
// method1();
//主类
在第二帧中:
//构造函数
class1 = new class1(); //运行构造函数2
//这里我得到一个NullPointerException错误
Let's say I run the first JFrame window and then from the 1st frame I open the 2nd frame. 1st frame is still open and now I want to access the first frame from the 2nd frame but I get NullPointerException if I use any methods or Objects from the fist frame. Even I looked online but all the videos are only talking about how to access 2nd frame from 1st frame. Please help me on this.
Thanks.
What I have tried:
In the first frame:
//constructor
class2 c = new class2();
//constructor2
method1();
//method1();
//main class
In the second frame:
//constructor
class1 = new class1();//runs the constructor2
//Here I get an NullPointerException error
推荐答案
你不应该创建一个新的class1
在class2
中,您应该使用对原始对象的引用。请参阅 Trail:学习Java语言(The Java™Tutorials) [ ^ ]。
You should not create a newclass1
inclass2
, you should use a reference to the original object. See Trail: Learning the Java Language (The Java™ Tutorials)[^].
这篇关于当我在第二个jframe时,如何访问第一个jframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!