关于在 Java Swing 桌面应用程序中使用 Window Listener 的两个问题
**** 请注意,我的问题是关于另一个线程中的答案。然而,当我在该帖子中发布问题时,它被删除了。所以我在这里重新发布这个问题(带有我所指的确切帖子的链接)。 ****
我有几个与此相关的问题 线程。如果我有一个计时器(updateTimer),我想在窗口关闭时取消它,我可以将其代替 System.out.println("Windows Closing");陈述?或者我是否必须将其放在实际的“View”类中(我有三个类DesktopApplication.App、DesktopApplication.View和DesktopApplication.AboutBox,并且配置Window方法位于.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
谢谢,祝你有美好的一天:)
帕特里克。 编辑以包含有关将存储的“用户名和密码”的信息。
**** Please note that my question is regarding the answers in another thread. However, when I posted the question in that thread, it was deleted. So I'm reposting the question here (with a link to the exact post that I'm referring to). ****
I have a couple of questions that go along with this thread. If I have a Timer (updateTimer), which I want to cancel when the window is closing, can I put that in place of the System.out.println("Windows Closing"); statement? Or would I have to put it in the actual "View" class (I have three classes DesktopApplication.App, DesktopApplication.View, and DesktopApplication.AboutBox and the configure Window method is in the .App class).
Along that line, if I can put the updateTimer.cancel(); line in, then does this mean I can read/write from a file, and popluate textboxes also (WindowOpen event) and write the information to the file in the closing event?
What I want to do is the following: When my application starts (and the main window opens) I want to check for a configuration file. If it exists, then I want to get the username, password, tunnel ID, and IP Address from that file--and populate their respective text boxes in the main jPanel. If it doesn't exist, then I won't do anything with it.
On closing the application, I want two things to happen: 1) any UpdateTimers that are running will be cancelled (to effectively and cleanly close the application) and 2) write the username, password, tunnel ID and IP Address to the configuration file for the next run.
I've created the file in Netbeans, so the "exitMenu" is automatically generated, and there is no "close button" configured. So I need to use WindowClosing to accomplish this (or hack the "exitMenu" method in a text editor and hope it doesn't create issues with Netbeans).
I should also add that the username and password are actually MD5 hashes of the real username and password. So, while someone can possibly open the text file and read them, they'll only see something like this:
c28de38997efb893872d893982ac
3289ab83ce8f398289d938999cab
12345
192.168.2.2
Thanks, and have a great day:)
Patrick.
Edited to include information about the "Username and Password" that will be stored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以在侦听器中放置任意代码
是的
Yes, you can put arbitrary code in your listener
Yes
我最终是如何实现这一目标的,就像这样。
在我的“TunnelbrokerUpdateView”类(实际处理主框架的类)中,我添加了以下代码:
我将其添加到“public TunnelbrokerUpdateView(SingleFrameApplication app)”方法中。所以,一切都按照我的意愿进行。我确信有更好的方法来合并用户信息,但这既快速又肮脏。将来,我确实计划对数据进行加密(或将其设置为通常无法读取的格式),因为涉及到密码哈希。
希望这能帮助其他人在未来。
(作为参考,这是整个方法(包括 Netbeans 自动放入的内容)
祝您有美好的一天:)
帕特里克.
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:
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)
Have a great day:)
Patrick.