1

**** 请注意,我的问题是关于另一个线程中的答案。但是,当我在该线程中发布问题时,它被删除了。所以我在这里重新发布这个问题(带有指向我所指的确切帖子的链接)。****

我有几个与这个线程相关的问题。如果我有一个计时器 (updateTimer),我想在窗口关闭时取消它,我可以用它代替 System.out.println("Windows Closing"); 陈述?或者我是否必须将它放在实际的“视图”类中(我有三个类 DesktopApplication.App、DesktopApplication.View 和 DesktopApplication.AboutBox,并且配置窗口方法在 .App 类中)。

沿着这条线,如果我可以把 updateTimer.cancel(); 行,那么这是否意味着我可以从文件读取/写入,也可以填充文本框(WindowOpen 事件)并在关闭事件中将信息写入文件?

我想做的是:当我的应用程序启动(并且主窗口打开)时,我想检查配置文件。如果它存在,那么我想从该文件中获取用户名、密码、隧道 ID 和 IP 地址——并在主 jPanel 中填充它们各自的文本框。如果它不存在,那么我不会对它做任何事情。

在关闭应用程序时,我希望发生两件事: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 回答