我想要在从一帧到另一帧的过渡中出现一个临时加载屏幕。 在大型机中,我创建了加载屏幕,该屏幕创建了另一个屏幕(就业框架)。它只是创建它,还没有显示它。
在就业框架中,我放置了一些loadingframe.setloadingbar()
方法,这些方法调用loadingframe中的setloadingbar
方法。在达到 100 之前,这一切都很完美。在 getvalue() == 100
时,它应该将 employmentframe
设置为可见,但它却给了我一个 nullpointerexception
。这很奇怪,因为就业屏幕是创建的。
代码如下 -
就业框架:
public EmploymentFrame(int eid, JFrame thisframe) {
initComponents();
//loadCaseFileList();
e_id = eid;
loadCourseList();
EmploymentFrame.thisframe = thisframe;
LoadingFrame.setLoadingBar(1);
}
public static void setEmploymentFrameVisible()
{
thisframe.setVisible(true);
}
加载框架:
private static JFrame Employmentframe;
private static int oldvalue;
private int e_id;
public LoadingFrame(int type, int eid) {
initComponents();
this.e_id = eid;
if(type == 1)
{
Employmentframe = new EmploymentFrame(eid, Employmentframe);
}
}
public static void setLoadingBar(int load)
{
oldvalue = LoadingBar.getValue();
System.out.println(""+oldvalue);
int newvalue = oldvalue+load;
System.out.println("nv"+newvalue);
LoadingBar.setValue(newvalue);
if(LoadingBar.getValue() == 100)
{
EmploymentFrame.setEmploymentFrameVisible();
}
}
谢谢。
最佳答案
堆栈跟踪表明该行抛出NPE
thisframe.setVisible(true);
所以thisframe
是null
。
当您在此处创建Employmentframe
时
Employmentframe = new EmploymentFrame(eid, Employmentframe);
您正在将 null
作为参数传递给构造函数,因为 JFrame
尚未初始化。事实上,EmploymentFrame
不需要传递其自身的实例。
还有许多其他问题:
- 静态方法被视为 poor design choice任何面向对象语言
- 多个
JFrame
被认为难以管理。首选替代方案是 1.) CardLayout在单个JFrame
上或 2。)在多个窗口需要单个JFrame
和模态JDialog
的情况下可以使用。还讨论了here . - Java 中的代码约定表明变量应以小写字母开头。
关于java - 加载屏幕 - JFrame 从其他类中设置可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14543753/