1

****私の質問は別のスレッドの回答に関するものであることに注意してください. しかし、そのスレッドに質問を投稿したところ、削除されました。そのため、ここに質問を再投稿します(参照している正確な投稿へのリンクを付けて)。****

このスレッドに関連する質問がいくつかあります。ウィンドウが閉じているときにキャンセルしたいタイマー (updateTimer) がある場合、それを System.out.println("Windows Closing"); の代わりに配置できますか? 声明?または、実際の「View」クラスに配置する必要があります (DesktopApplication.App、DesktopApplication.View、および DesktopApplication.AboutBox の 3 つのクラスがあり、configure Window メソッドは .App クラスにあります)。

その行に沿って、 updateTimer.cancel(); を置くことができれば。これは、ファイルからの読み取り/書き込み、テキストボックスへの入力 (WindowOpen イベント)、および終了イベントでのファイルへの情報の書き込みができることを意味しますか?

私がやりたいことは次のとおりです: アプリケーションが起動する (そしてメイン ウィンドウが開く) ときに、構成ファイルを確認したいと考えています。存在する場合は、そのファイルからユーザー名、パスワード、トンネル ID、および IP アドレスを取得し、メインの jPanel にそれぞれのテキスト ボックスを入力します。存在しない場合は、何もしません。

アプリケーションを閉じるときに、次の 2 つのことを実行する必要があります。1) 実行中の UpdateTimers はすべてキャンセルされ (アプリケーションを効果的かつクリーンに閉じるため)、2) ユーザー名、パスワード、トンネル ID、および IP アドレスを構成ファイルに書き込みます。次の実行。

Netbeans でファイルを作成したので、「exitMenu」が自動的に生成され、「閉じるボタン」が構成されていません。そのため、これを実現するには WindowClosing を使用する必要があります (または、テキスト エディターで「exitMenu」メソッドをハックして、Netbeans で問題が発生しないことを願っています)。

また、ユーザー名とパスワードは、実際には実際のユーザー名とパスワードの MD5 ハッシュであることも付け加えておきます。そのため、誰かがテキスト ファイルを開いて読むことはできますが、次のような内容しか表示されません。

c28de38997efb893872d893982ac 3289ab83ce8f398289d938999cab 12345 192.168.2.2

ありがとう、そして素晴らしい一日を:)

パトリック。保存される「ユーザー名とパスワード」に関する情報を含めるように編集しました。

4

2 に答える 2

1

System.out.println( "Windows Closing");の代わりにそれを配置できますか?声明?

はい、リスナーに任意のコードを挿入できます

その線に沿って、updateTimer.cancel();を置くことができれば; 行入力すると、これは、ファイルから読み取り/書き込みが可能であり、テキストボックスにもデータを入力して(WindowOpenイベント)、終了イベントでファイルに情報を書き込むことができることを意味しますか?

はい

于 2011-04-25T14:11:42.727 に答える
0

How I ended up accomplishing this is like this.

In my "TunnelbrokerUpdateView" class (the one that actually handles the main frame), I added the following code:

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

I added this into the "public TunnelbrokerUpdateView(SingleFrameApplication app)" method. So, everything works as I wanted it to. I'm sure there are better ways of incorporating the user information, but this was quick and dirty. In the future, I do plan on encrypting the data (or making it into a format that isn't readable normally), since there's a password hash involved.

Hopefully this will help someone else in the future.

(for reference, here's the entire method (including the stuff that Netbeans automatically puts in)

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

Have a great day:) Patrick.

于 2011-05-01T05:32:17.853 に答える