简体   繁体   English

如何使用JFrame关闭调用的类而不关闭调用它的类?

[英]How to close a called class using JFrame without closing the class that called it?

I seem to have a fairly unique problem, and I searched for a while for an answer on here without finding one. 我似乎有一个非常独特的问题,我在这里搜索了一段时间,但没有找到答案。 I have a class that has a simple JFrame with two buttons. 我有一个带有两个按钮的简单JFrame的类。 Each button calls the Main method of a different class, as such: 每个按钮都调用不同类的Main方法,例如:

checkRuling = new JButton("Check Your Deck's Rulings");
  checkRuling.addActionListener(new ActionListener() {
     public void actionPerformed (ActionEvent e) {
        ReadHtmlCardDatabase.main(null);
     }
  });

One calls a class that takes a series of inputs into a text field and creates a formatted html document from the inputs, and the other loads the html document into a JEditorPane. 一个调用一个类,该类将一系列输入输入到文本字段中,并根据这些输入创建格式化的html文档,另一个调用该类将html文档加载到JEditorPane中。 My problem is that when I close one of the JFrames for the subclasses (either the input or html loader one), it exits my program completely, and I want to keep the main class (with the two buttons) open. 我的问题是,当我关闭一个用于子类的JFrame(输入或html加载器之一)时,它完全退出了程序,并且我想保持主类(使用两个按钮)打开。 I've tried using: 我试过使用:

close = new JButton("CLOSE");
     close.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent e) {
          System.exit(1);
        }
     });

On a button in the subclasses, to no avail. 在子类中的按钮上无济于事。 When the button is clicked it simply exits everything. 单击该按钮时,它仅退出所有内容。 I've also tried using: 我也尝试过使用:

JFrame.DISPOSE_ON_EXIT

For the subclasses, but this causes the JFrames to go away without the subclasses actually closing, so the first one that saves the html document never actually saves it, and the second subclass that opens that same html document won't work, because it wasn't saved. 对于子类,但这会导致JFrames消失而没有真正关闭子类,因此第一个保存html文档的文件实际上并没有保存它,而第二个打开该html文档的子类将无法工作,因为没有保存。 Any help would be appreciated, because I can't figure out how to do this. 任何帮助将不胜感激,因为我不知道如何做到这一点。

As Fast Snail says in the comments, you shouldn't be calling a main method. 就像Fast Snail在评论中所说的那样,您不应该调用main方法。 Instantiate a class that does each functonality. 实例化完成每个功能的类。 Set the frame to visible using setVisible(true) when you start using it, then setVisible(false) when you're done. 开始使用时,使用setVisible(true)将框架设置为可见,然后在完成时使用setVisible(false) So, in the action listener, just change the visibility. 因此,在动作侦听器中,只需更改可见性即可。

Then, assuming you don't have anything too wild going on, the frame you just set to invisible should go out of scope and get freed so that memory isn't chewed up. 然后,假设您没有任何异常处理,刚设置为不可见的帧应超出范围并释放,以免占用内存。 You just instantiate a new copy of the ReadHtmlCardDatabase class each time you need one. 每次需要时,您只需实例化ReadHtmlCardDatabase类的新副本。 Or you could have one static copy that you set visible/invisible as needed. 或者,您可以拥有一个静态副本,并根据需要将其设置为可见/不可见。

one of the JFrames JFrame之一

You should use only one JFrame in Your GUI. 您应该在GUI中仅使用一个JFrame。 For other windows You can use for example JDialog or JWindow. 对于其他窗口,您可以使用例如JDialog或JWindow。 This should help, if not You can always use frame.setVisible(false) instead of dispose on close, but it' s not very neat. 如果没有,这应该会有所帮助。您可以始终使用frame.setVisible(false)而不是放在close上,但这不是很整齐。

Thanks to someone who posted a comment and then deleted it, I've figured out my own problem. 多亏有人发表评论然后将其删除,我才解决了自己的问题。 I just had to replace my main call with this: 我只需要用以下内容代替我的主要电话:

setDeck = new JButton("Set Deck");
  setDeck.addActionListener(new ActionListener() {
     public void actionPerformed (ActionEvent e) {
        WriteHtmlCardDatabase w = new WriteHtmlCardDatabase();
        w.main(null);
     }
  });

Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM