关于在Java Swing桌面应用程序中使用窗口侦听器的两个问题



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

我有几个问题,随着这个线程。如果我有一个定时器(updateTimer),我想在窗口关闭时取消,我能把它放在System.out的地方吗?println("窗户关闭");声明吗?或者我必须把它放在实际的"视图"类中(我有三个类DesktopApplication。应用,DesktopApplication。View和DesktopApplication。AboutBox和configure Window方法在。app类中)。

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

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

在关闭应用程序时,我想要发生两件事:1)任何正在运行的UpdateTimers将被取消(有效和干净地关闭应用程序)和2)将用户名,密码,隧道ID和IP地址写入下次运行的配置文件。

我已经在Netbeans中创建了这个文件,所以"exitMenu"是自动生成的,并且没有配置"关闭按钮"。所以我需要使用WindowClosing来完成这一点(或在文本编辑器中破解"exitMenu"方法,并希望它不会与Netbeans产生问题)。

我还应该补充说,用户名和密码实际上是真实用户名和密码的MD5哈希值。因此,虽然有人可能打开文本文件并读取它们,但他们只会看到这样的内容:

c28de38997efb893872d893982ac3289年ab83ce8f398289d938999cab12345192.168.2.2

谢谢,祝你有美好的一天。

帕特里克。编辑以包含将被存储的"用户名和密码"信息

我可以把它放在System.out的位置吗?println("窗户关闭");声明吗?

是的,您可以在侦听器中放入任意代码

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

是的

我最终是这样完成的。

在我的"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);
}

我将其添加到"public 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);
}

祝你今天愉快:)帕特里克。

相关内容

  • 没有找到相关文章

最新更新