知道什么时候退出不同的班级

时间:2016-08-09 19:04:34

标签: java swing

我目前正在eclipse中编写一个项目Java,它有两个类。第一个类(open)用于将特定字符串发送到我的第二个类(查看器),然后运行我的第二个类。第二个类(查看器)我以jar文件的形式导入到我的程序中。我这样做是因为类查看器是我使用apache创建的pdf查看器PDFBox和类打开将文件发送给查看器使用,但文件将根据许多条件(不相关)在开放时打开。关键是开放类需要与类查看器分开,并且不能简单地在一个类中使用两种不同的方法。我想知道是否有一种方法可以让类打开以了解类查看器何时被关闭,因为目前我正在使用while循环,这只会占用内存并且效率非常低。我现在的代码目前有效,但我觉得有更好的方法,也许是使用听众。这是关闭类查看器的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; 
//and import swing components ect

Public class viewer extends javax.swing.JFrame
implements KeyListener,
ActionListener{
    private javax.swing.JButton zoomIn;
    private javax.swing.JButton zoomOut;
    //and a bunch more swing components
    public static boolean closed = false;
    public static String fileName = "";
    public viewer()
    {
    }

private void initComponents() throws IOException
{
  addWindowListener(new java.awt.event.WindowAdapter()
    {
        @Override
        public void windowClosing(java.awt.event.WindowEvent evt)
        {
            exitApplication();
        }
    });
}

private void exitMenuItemActionPerformed(ActionEvent evt)
{
    if( document != null )
    {
        try
        {
            document.close();
        }
        catch( IOException e )
        {
            throw new RuntimeException(e);
        }
    }
    closed = true;
    System.exit(0);
}
public static void main(String filename) throws Exception
{
    fileName = filename;
    viewer mainViewer = new viewer();
    String[] splittedStr = fileName.split("/");
    BASETITLE = splittedStr[splittedStr.length - 1];
    if (fileName != null)
    {
        mainViewer.openPDFFile(fileName);
    }
    mainViewer.setVisible(true);
}

这是我打开课程的代码:

public static void main(String[] args) throws Exception {
    String fileName = "C:/Files/Test.pdf";
    viewer.main(fileName);
    while(viewer.closed == false)
    {
        if(viewer.closed == true)
        {
            System.out.print("The Viewer Has Been Closed");
        }
    }
}

我想知道它什么时候关闭,所以我可以删除本地驱动器上的文件。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

要么将回调(例如Runnable)作为参数传递给viewer.main,例如viewer.main(fileName,() - > System.out.print(“查看器已关闭”))并确保在进程完成时调用它,或者你已经完成了,除了你在while循环中短时间内睡眠你的主线程,比如Thread.sleep(100)。

答案 1 :(得分:0)

好的,所以我实际上找到了我的问题的答案,我会发布它,以防其他人遇到与我相同的问题。我在open类中添加了一个接口,方法是close,如下所示:

   lnkNm = InputBox("please enter link description")
   Application.ScreenUpdating = False
   fdlgType = 4

   set fdlg = Application.FileDialog(fdlgType)

    With fdlg
    .Title = IIf(fdlgType = 3, "Please select an Evidence FILE to link to", _
                               "Please select an Evidence FOLDER to link to")
    .ButtonName = IIf(fdlgType = 3, "Select File", "Select Folder")
    .Show
    For Each itm In .SelectedItems
        strFileToLink = itm
          'Checking if file is selected.
        If strFileToLink = vbNullString Then
            'Displaying a message if file not choosen in the above step.
            MsgBox "No file selected.", vbExclamation, "Sorry"
            'And exiting from the procedure.
            Exit Sub
        Else

这是我的界面代码:

import mainpackage.viewer;
public class Open implements closeInterface{

    public Open() { }
    public static String fileName;
    public static void main(String[] args) throws Exception {
        fileName = "C:/Files/Test.pdf";
        run();
    }

    @Override
    public void close() {
        System.out.print("The Viewer Has Been Closed");

    }

    public static void run() throws Exception
    {
        viewer view = new viewer();
        view.main(fileName);
        view.addListener(new Open());
    }
}

这是我的Viewer类的代码片段

package mainpackage;

public interface closeInterface {
    public void close();
}

感谢大家的帮助!