加载屏幕 - 可从其他类设置的JFrame

时间:2013-01-27 02:09:29

标签: java swing nullpointerexception jframe

我希望在从一帧到另一帧的过渡中有一个临时加载屏幕。 在大型机中,我创建了加载屏幕,创建了另一个屏幕(就业框架)。它只创建它,它还没有显示它。

在就业框架中,我添加了一些loadingframe.setloadingbar()方法,用于在loadingframe中调用setloadingbar方法。这种方法很有效,直到达到100. getvalue() == 100它应该employmentframe可见,但它会给我一个nullpointerexception。这很奇怪,因为创造了就业屏幕。

代码如下 -

Employmentframe:

   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();
       }
   }

感谢。

1 个答案:

答案 0 :(得分:1)

stacktrace表示此行正在抛出NPE

thisframe.setVisible(true);

所以thisframenull

在此处创建Employmentframe

Employmentframe = new EmploymentFrame(eid, Employmentframe); 

您将null作为参数传递给构造函数,因为JFrame尚未初始化。实际上,EmploymentFrame不需要传递自己的实例。


还有许多其他问题:

  • 静态方法在任何OO语言中都被视为poor design choice
  • 多个JFrames被认为难以管理。首选备选方案是单个JFrame上的1.)CardLayout或2.)如果需要多个窗口 ,则需要一个JFrame个模式JDialog可以使用。还讨论了here
  • Java中的代码约定表明变量应以小写字母开头。
相关问题