从另一个方法关闭JFrame

时间:2016-06-27 21:50:28

标签: java methods jframe jpanel

此加载窗口打开,我想在closeLoadingWindow方法运行后关闭它。我有一个按钮,在另一个类中触发closeLoadingWindow方法。任何帮助?

import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LoadingScreen {


  public static void createLoadingWindow()
  {
   JFrame LoadingFrame = new JFrame("Loading");
   LoadingFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

   JPanel LoadingPanel = new JPanel();
   LoadingFrame.add(LoadingPanel, BorderLayout.NORTH);

   ImageIcon LoadingGif = new ImageIcon();
   JLabel LoadingLabel = new JLabel(LoadingGif);
   LoadingPanel.add(LoadingLabel);
   LoadingFrame.setVisible(true);
  }

  public static void closeLoadingWindow()
  {
     dispose();
  }
}

1 个答案:

答案 0 :(得分:0)

也许如果您将加载框初始化为:

public class LoadingScreen{
    JFrame LoadingFrame;
    public void createLoadingWindow(){
          LoadingFrame = new JFrame("Loading");
          ...
    }
    public void closeLoadingWindow(){
         LoadingFrame.dispose();
    }
}

或者您可以在关闭的LoadingWindow()方法中将Frame Loading帧作为参数传递:

public void closeLoadingWindow(JFrame LoadingFrame){
     LoadingFrame.dispose();
}