2011-04-25 55 views
1

****請注意,我的問題是關於另一個線程的答案。但是,當我在該線程中發佈該問題時,該問題已被刪除。所以我在這裏重新發布這個問題(鏈接到我指的確切文章)。 ****兩個問題擺動桌面應用程序

我有幾個與此thread走的問題。如果我有一個定時器(updateTimer),我想在窗口關閉時取消它,可以將它放在System.out.println(「Windows Closing」)的位置上嗎?聲明?或者我必須把它放在實際的「View」類中(我有三個類DesktopApplication.App,DesktopApplication.View和DesktopApplication.AboutBox,配置Window方法在.App類中)。

沿該線,如果我可以把updateTimer.cancel(); line in,那麼這是否意味着我可以從文件讀取/寫入,並且還可以在文本框中彈出文本框(WindowOpen事件)並將信息寫入文件中?

我想要做的是:當我的應用程序啓動(與主窗口中打開)我要檢查的配置文件。如果它存在,那麼我想從該文件中獲取用戶名,密碼,隧道ID和IP地址 - 並在主jPanel中填充它們各自的文本框。如果它不存在,那麼我就不會做任何事情。

在關閉應用程序時,我想要發生兩件事情:1)任何正在運行的UpdateTimers都將被取消(以有效且乾淨地關閉應用程序)並且2)將用戶名,密碼,隧道ID和IP地址寫入下一次運行的配置文件。

我創建NetBeans中的文件,因此「exitMenu」是自動生成的,並沒有「關閉按鈕」配置。所以我需要使用WindowClosing來完成此操作(或者在文本編輯器中破解「exitMenu」方法,並希望它不會與Netbeans產生問題)。

我還要補充一點,用戶名和密碼實際上是真實的用戶名和密碼的MD5哈希值。因此,雖然可以有人可能打開文本文件,並宣讀他們,他們只能看到這樣的事情:

c28de38997efb893872d893982ac 3289ab83ce8f398289d938999cab 192.168.2.2

感謝,並有一個偉大的日子:)

Patrick。 修改爲包含有關將要存儲的「用戶名和密碼」的信息。

+0

您肯定可以在窗口關閉事件中取消定時器。 – MeBigFatGuy 2011-04-25 13:05:36

+0

感謝您的回答。我想我的問題確實是「這需要進入哪個類文件?」試圖將它放入App文件似乎不起作用(@Override配置Windows方法所在的位置)。所以我認爲我必須將其手動編碼到「View」類文件(顯示主JPanel和其他所有文件的文件)中。 – PatrickDickey 2011-04-26 20:02:20

回答

1

我可以把它放在System.out.println(「Windows Closing」)的位置嗎?聲明?

是的,你可以把任意代碼在你的聽衆

沿該線,如果我可以把updateTimer.cancel(); line in,那麼這是否意味着我可以從文件讀取/寫入,並且還可以在文本框中彈出文本框(WindowOpen事件)並將信息寫入文件中?

+0

謝謝你的快速回答。我會嘗試它,但我應該問我是否需要做任何特殊的事情(因爲UpdateTimer的聲明在.View文件中)。 – PatrickDickey 2011-04-25 19:44:49

+0

好的,所以我昨天試了這個,但沒有奏效。我認爲最後,我必須將代碼放入TunnelBrokerUpdateView.java類文件(而不是其他線程中建議的TunnelBrokerUpdateApp.java類文件)。 主要問題是Netbeans 6.9.1並不容易。當你點擊主JPanel時,你會認爲他們會有「窗口監聽器」或「窗口關閉」的右鍵單擊選項。不。所以,現在我必須手動添加偵聽器,並希望我可以把它放在正確的位置(因爲initComponents()方法是由表單自動生成的)。 – PatrickDickey 2011-04-26 19:57:47

0

我是如何結束完成這個就是這個樣子。

在我的「TunnelbrokerUpdateView」級(實際處理主框架的),我添加以下代碼:

WindowListener wl = new WindowListener(){ 

     public void windowOpened(WindowEvent e) 
     { 
      try 
      { 
        FileReader fr = new FileReader (new File("userinfo.txt")); 
        BufferedReader br = new BufferedReader (fr); 
        jTextField1.setText(br.readLine()); 
        jPasswordField1.setText(br.readLine()); 
        jTextField2.setText(br.readLine()); 
        oldIPAddress = br.readLine(); 
        br.close(); 
      } 
      catch (FileNotFoundException ex) { 
        // Pop up a dialog box explaining that this information will be saved 
       // and propogated in the future.. "First time running this?" 
        int result = JOptionPane.showConfirmDialog((Component) 
      null, "After you enter your user information, this box will no longer show.", "First Run", JOptionPane.DEFAULT_OPTION); 
     } 
      catch (java.io.IOException ea) 
      { 
       Logger.getLogger(TunnelbrokerUpdateView.class.getName()).log(Level.SEVERE, null, ea); 
      } 
     } 

     public void windowClosing(WindowEvent e) { 
      updateTimer.cancel(); 
      BufferedWriter userData; 

      //Handle saving the user information to a file "userinfo.txt" 
      try 
      { 
       userData = new BufferedWriter(new FileWriter("userinfo.txt")); 
       StringBuffer sb = new StringBuffer(); 
       sb.append(jTextField1.getText()); 
       sb.append(System.getProperty("line.separator")); 
       sb.append(jPasswordField1.getText()); 
       sb.append(System.getProperty("line.separator")); 
       sb.append(jTextField2.getText()); 
       sb.append(System.getProperty("line.separator")); 
       sb.append(oldIPAddress); 
       userData.write(sb.toString()); 
       userData.close(); 

      } 
      catch (java.io.IOException ex) 
      { 
       Logger.getLogger(TunnelbrokerUpdateView.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     } 

     public void windowClosed(WindowEvent e) { 
      System.exit(0); 
     } 

     public void windowIconified(WindowEvent e) {} 

     public void windowDeiconified(WindowEvent e) {} 

     public void windowActivated(WindowEvent e) {} 

     public void windowDeactivated(WindowEvent e) {} 

    }; 
    super.getFrame().addWindowListener(wl); 
} 

我加入到這個「公共TunnelbrokerUpdateView(SingleFrameApplication APP)」方法。所以,一切都按我想要的那樣工作。我相信有更好的方法來整合用戶信息,但這很快且很髒。將來,我會計劃對數據進行加密(或使其成爲通常無法讀取的格式),因爲存在密碼散列。

希望這將有助於未來的其他人。

(僅供參考,這裏的整個方法(包括NetBeans將自動放入的東西)

public TunnelbrokerUpdateView(SingleFrameApplication app) { 
    super(app); 


    initComponents(); 




    // status bar initialization - message timeout, idle icon and busy animation, etc 
    ResourceMap resourceMap = getResourceMap(); 
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout"); 
    messageTimer = new Timer(messageTimeout, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      statusMessageLabel.setText(""); 
     } 
    }); 
    messageTimer.setRepeats(false); 
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate"); 
    for (int i = 0; i < busyIcons.length; i++) { 
     busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]"); 
    } 
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      busyIconIndex = (busyIconIndex + 1) % busyIcons.length; 
      statusAnimationLabel.setIcon(busyIcons[busyIconIndex]); 
     } 
    }); 
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon"); 
    statusAnimationLabel.setIcon(idleIcon); 
    progressBar.setVisible(false); 

    // connecting action tasks to status bar via TaskMonitor 
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); 
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() { 
     public void propertyChange(java.beans.PropertyChangeEvent evt) { 
      String propertyName = evt.getPropertyName(); 
      if ("started".equals(propertyName)) { 
       if (!busyIconTimer.isRunning()) { 
        statusAnimationLabel.setIcon(busyIcons[0]); 
        busyIconIndex = 0; 
        busyIconTimer.start(); 
       } 
       progressBar.setVisible(true); 
       progressBar.setIndeterminate(true); 
      } else if ("done".equals(propertyName)) { 
       busyIconTimer.stop(); 
       statusAnimationLabel.setIcon(idleIcon); 
       progressBar.setVisible(false); 
       progressBar.setValue(0); 
      } else if ("message".equals(propertyName)) { 
       String text = (String)(evt.getNewValue()); 
       statusMessageLabel.setText((text == null) ? "" : text); 
       messageTimer.restart(); 
      } else if ("progress".equals(propertyName)) { 
       int value = (Integer)(evt.getNewValue()); 
       progressBar.setVisible(true); 
       progressBar.setIndeterminate(false); 
       progressBar.setValue(value); 
      } 
     } 
    }); 

    // This will take care of Opening and Closing 
    WindowListener wl = new WindowListener(){ 

     public void windowOpened(WindowEvent e) 
     { 
      try 
      { 
        FileReader fr = new FileReader (new File("userinfo.txt")); 
        BufferedReader br = new BufferedReader (fr); 
        jTextField1.setText(br.readLine()); 
        jPasswordField1.setText(br.readLine()); 
        jTextField2.setText(br.readLine()); 
        oldIPAddress = br.readLine(); 
        br.close(); 
      } 
      catch (FileNotFoundException ex) { 
        // Pop up a dialog box explaining that this information will be saved 
       // and propogated in the future.. "First time running this?" 
        int result = JOptionPane.showConfirmDialog((Component) 
      null, "After you enter your user information, this box will no longer show.", "First Run", JOptionPane.DEFAULT_OPTION); 
     } 
      catch (java.io.IOException ea) 
      { 
       Logger.getLogger(TunnelbrokerUpdateView.class.getName()).log(Level.SEVERE, null, ea); 
      } 
     } 

     public void windowClosing(WindowEvent e) { 
      updateTimer.cancel(); 
      BufferedWriter userData; 

      //Handle saving the user information to a file "userinfo.txt" 
      try 
      { 
       userData = new BufferedWriter(new FileWriter("userinfo.txt")); 
       StringBuffer sb = new StringBuffer(); 
       sb.append(jTextField1.getText()); 
       sb.append(System.getProperty("line.separator")); 
       sb.append(jPasswordField1.getText()); 
       sb.append(System.getProperty("line.separator")); 
       sb.append(jTextField2.getText()); 
       sb.append(System.getProperty("line.separator")); 
       sb.append(oldIPAddress); 
       userData.write(sb.toString()); 
       userData.close(); 

      } 
      catch (java.io.IOException ex) 
      { 
       Logger.getLogger(TunnelbrokerUpdateView.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     } 

     public void windowClosed(WindowEvent e) { 
      System.exit(0); 
     } 

     public void windowIconified(WindowEvent e) {} 

     public void windowDeiconified(WindowEvent e) {} 

     public void windowActivated(WindowEvent e) {} 

     public void windowDeactivated(WindowEvent e) {} 

    }; 
    super.getFrame().addWindowListener(wl); 
} 

有一個偉大的一天:) 帕特里克。