执行System.exit(0)之前方法无法正常工作

时间:2013-07-31 15:15:24

标签: java

我是Java的新手,我有一个菜单文件/退出的swing应用程序,应用程序是一个编辑器,当我从菜单中选择退出选项时,应用程序会询问我是否要在退出前保存更改,请参阅下面的退出代码

class ActionExit extends AbstractAction
{
public ActionExit()
{
  super("Exit",Icons.iconExit);
  setEnabled(true);
  putValue("MENU", "File");
  putValue("MENUINDEX", "99");
}

public void actionPerformed(ActionEvent evt)
{
  saveandClose(); <----does not execute all the instructions
  saveSettings();
  System.exit(0);
  }
}

我遇到的问题是方法saveandClose(),这里我检查文档是否已更改并显示带有问题/选项的对话框(保存?是,否),我还执行其他连接的方法到数据库并应用我对文档等所做的更改。此方法仅执行第一条指令然后似乎被中断(我放了很多&#34; System.out.println&#34;来查找方法停止的位置),然后应用程序返回并继续下一个方法并结束。 如果我调试应用程序一切正常,但它运行正常时不起作用。为什么会发生这种情况?,在退出之前,我该怎么做才能使应用程序保存更改?

**这是saveandClose **

的代码

基本上我在这里做的是,从主框架我试图找到我要关闭的组件(面板),一旦找到它我执行指令:

(PanelMember)组分[Y])。closeTab()

我重复使用此方法,它也在菜单(窗口/关闭面板)中的另一个选项中调用,当我在包含文档的面板中单击并选择&#39; X&#39;关闭它

public void saveandClose() {

      Component[] component =this.getComponents();
        for (int y = 0; y < component.length; y++) {
        if ((component[y] instanceof Container)) {
             saveandClose2((Container)component[y]);
             }
           }
       }

  public void saveandClose2(Container pcont) {
      Component[] component = pcont.getComponents();
        for (int y = 0; y < component.length; y++) {
        if ((component[y] instanceof Container)) {
           if ((component[y] instanceof PanelMember)) {
               ((PanelMember)component[y]).closeTab();
             }
              closeTabsinPanel2((Container)component[y]);
          }
       }
  }

以下是执行的其他程序/方法,谢谢你的帮助

PanleMember.java

public void closeTab()
  {
    Environment.members.close(projectMember, false);
  }

Members.java

public class Members
{
  public void close(ProjectMember projectMember, boolean cache)
  {
    if (Environment.toolManager == null) {
      return;
    }
    if (!projectMember.getMember().isOkToClose()) {
      return;
    }
    Environment.toolManager.close(projectMember, cache);
  }

Member.java

public boolean isOkToClose()
    {
        ListenerMember temp[] = (ListenerMember[])listListeners.toArray(new ListenerMember[listListeners.size()]);
        for(int x = 0; x < temp.length; x++)
        {
            if(!temp[x].isOkToClose(this))
                return false;
        }

        return true;
    }

PanelMember.java - 这是我显示对话框以询问用户是否保存更改

public boolean isOkToClose(Member member)
 {
   if (currentlySaving) {
     return false;
   }
   if (sourceParser.isDirty()) {
     int option = JOptionPane.showConfirmDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(), projectMember.getMember().getName() + " has been changed. Would you like to save the Changes?", "Save Changes", 1, 3);
      if (option == 2) {
        return false;
      }
      if (option == 0)
      {
        closeAfterSave = true;
        startSave();
        return false;
      }
    }
    return true;
  }


public void startSave()
   {
     final String procedureLibrary;
     try
     {
       procedureLibrary = getProcedureLibrary();
     }
     catch (SQLException e) {
       JOptionPane.showMessageDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(), e.getMessage());
      e.printStackTrace();
      return;
    }
    if (procedureLibrary == null) {
      return;
    }

        System.out.println("Entering startSave method");  //gs01
        selectedTabBeforeSave = jTabbedPane1.getSelectedIndex();

        Thread t = new Thread() {
         //   private final String val$procedureLibrary;

          public void run() { setPriority(1);
            try {
              save(procedureLibrary);
            }
            catch (Exception e) {
              JOptionPane.showMessageDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(), e.getMessage());
               e.printStackTrace();
             }
           }
         };
         t.start();
       }

protected void save(String procedureLibrary)
    throws SQLException
      {
        if (currentlySaving) {
          return;
        }
    currentlySaving = true;
    actionMemberSave.setEnabled(false);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        panelLoading.setText("Saving Source.");
        jTabbedPane1.insertTab("Status", null, panelStatus, "Status", 0);
       jTabbedPane1.setSelectedIndex(0);
       jTabbedPane1.setEnabled(false);
     }
   });
   projectMember.member.save(procedureLibrary, sourceParser, this);
   currentlySaving = false;
   if (closeAfterSave)
     Environment.members.close(projectMember, false); 
 }

Member.java - 这里我放了很多System.out.println来查看进程停止的位置,有时显示的最后一条消息是&#34; conection:&#34;,有时是&#34;同步&# 34;其余部分永远不会显示

public void save(String procedureLibrary, SourceParser parser, ListenerSave listener)
    {
       System.out.println("Entering save method");  //gs01
        saveBulk(parser, listener);
    }


public void saveBulk(SourceParser parser, ListenerSave listener)
    {
        Connection connection;
        Statement stmt;
        StringBuffer buffer;
        String backup;
        String append;
        int today, date, row;
        buffer = new StringBuffer();
        backup = null;
        append = " ";
        row = 1;
        System.out.println("Entering saveBulk method");  //gs01
        for(SourceLine line = parser.getFirst(); line != null; line = line.getNext())
        {
            if(line.getText().length() > maxChars + 1)
            {
                if(listener != null) {
                   listener.saveComplete(0, false, "Line number: " + row + " is over " + maxChars + " characters.");
                }
                return;
            }
            row++;
        }

        try {

        System.out.println("Backup file saved to :" + saveBackup(parser));  //gs01
        backup = "Backup file saved to: " + saveBackup(parser);
        row = 0;
        Calendar cal = Calendar.getInstance();
        today = (cal.get(1) - 2000) * 10000;
        today += (cal.get(2) + 1) * 100;
        today += cal.get(5);
        connection = as400system.getConnection(); //sometimes the process stops here
        System.out.println("connection :" + connection);  //gs01
        synchronized(connection)
        {
            System.out.println("synchronized");  //sometimes the process stops here
            as400system.createTempSrcTable(maxChars + 12);
            System.out.println("create temp src table  ");  //gs01
            stmt = connection.createStatement(); 
            System.out.println("conection create statement");  
            for(SourceLine line = parser.getFirst(); line != null; line = line.getNext ())
            {
                buffer.append(line.getText());
                date = line.date;
                if(line.changed || line.created) {
                    date = today;
                }
                line.changed = false;
                line.created = false;
                line.date = date;
                buffer.append(line.date);
                buffer.append("\n");
                row++;
                if(buffer.length() > 32000)
                {
                  System.out.println("if buffer.length>32000 " );  //gs01
                    stmt.execute("call qgpl/prcupload('" + buffer.toString().replaceAll("'", "''") + "', '\n', '" + append + "')");
                    append = "T";
                    buffer = new StringBuffer();
                    if(listener != null) {
                        listener.lineSaved(row);
                    }
               }
          } 
            System.out.println("if buffer.length>0 " );  //gs01
            if(buffer.length() > 0 || append.equalsIgnoreCase(" ")) {
                System.out.println("call qgpl/prcupload");  //gs01
                stmt.execute("call qgpl/prcupload('" + buffer.toString().replaceAll("'", "''") + "', '\n', '" + append + "')");
            }
            stmt.execute(as400system.buildSqlForCmd("CPYF FROMFILE(QTEMP/SRCUPLOAD)    TOFILE(" + library + "/" + file + ") TOMBR(" + member + ") MBROPT(*REPLACE) FMTOPT(*NOCHK)"));
            stmt.close();
            System.out.println("File copied :" + member);  //gs01
        }
        if(listener != null) {
            System.out.println("saveComplete method");  //gs01
            listener.saveComplete(row, true, backup);
        }
        System.out.println("parser setdirty");  //gs01
        parser.setDirty(false);
        }

        catch (Exception e) {
        System.out.println("buffer: (" + buffer.toString().replaceAll("'", "''") + ")");
        e.printStackTrace();
        if(listener != null) {
            listener.saveComplete(0, false, e.getMessage() + "\n" + backup);
        }
    }
       System.out.println("nothing");  //gs01
  }     

0 个答案:

没有答案