简体   繁体   English

用按钮关闭窗口

[英]Closing a Window with Button

I am currently studying Java to improve myself. 我目前正在学习Java以提高自我。 I have a program which has a main window, menu and submenus. 我有一个包含主窗口,菜单和子菜单的程序。

I have other windows on when I click on my submenus. 单击子菜单时,我还有其他窗口。

One of them is setRates which is 其中之一是setRates,这是

public SetMyRates(){
    JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
    dataPanel.add(setTLLabel);
    dataPanel.add(setDollarsLabel);
    dataPanel.add(setTLField);
    dataPanel.add(setDollarsField);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(closeButton);
    buttonPanel.add(setTLButton);
    buttonPanel.add(setDollarsButton);
    Container container = this.getContentPane();
    container.add(dataPanel, BorderLayout.CENTER);
    container.add(buttonPanel, BorderLayout.SOUTH);
    setTLButton.addActionListener(new SetTL());
    setDollarsButton.addActionListener(new SetDollars());
    closeButton.addActionListener(new closeFrame());
    dataPanel.setVisible(true);
    pack();
}

and I want that window to close when I click on my closeButton . 当我单击closeButton时,我希望该窗口关闭。

I made a class for closeButton, actionListener which is: 我为closeButton,actionListener创建了一个类:

private class closeFrame implements ActionListener{
    public void actionPerformed(ActionEvent e){
       try{
          dispose();
       }
       catch(Exception ex){
          JOptionPane.showMessageDialog(null, "Please enter correct Rate.");
       }
    }
}

But when I click that button, it closes my main window instead of my submenus window. 但是,当我单击该按钮时,它将关闭我的主窗口而不是子菜单窗口。 What should I exactly do to fix the problem? 我应该怎么做才能解决该问题?

You need to get a reference to the Window that you want to close and call dispose() directly on that reference. 您需要获取要关闭的Window的引用,然后直接对该引用调用dispose() How you do this will depend on the details of your program -- information that we're currently not privy to. 您如何执行此操作将取决于您程序的详细信息-我们目前尚不了解这些信息。

Edit: one way to get that reference is via SwingUtilities.getWindowAncestor(...) . 编辑:一种获取该引用的方法是通过SwingUtilities.getWindowAncestor(...) Pass in the JButton reference returned from your ActionEvent object and call dispose on it. 传递从ActionEvent对象返回的JButton参考,并对其进行调用dispose。 Something like... 就像是...

public void actionPerformed(ActionEvent e) {
  Object o = e.getSource();
  if (o instanceof JComponent) { 
    JComponent component = (JComponent)o; 
    Window win = SwingUtilities.getWindowAncestor(component);
    win.dispose();
  }
}
  • caveat: code neither compiled nor run nor tested in any way. 警告:代码既不编译,也不以任何方式运行或测试。
  • Also note that for this to work, the component that holds and activates the ActionListener has to reside on the Window that you wish to close, else this won't work. 还要注意,要使此功能正常运行,保存并激活ActionListener的组件必须驻留在您希望关闭的Window上,否则将无法正常工作。

From what I think you could easily when opening an another window just store a reference to it and use it inside the action listener. 根据我的想法,打开另一个窗口时,您可以轻松地存储对它的引用,并在动作监听器中使用它。 Something along these lines: 遵循以下原则:

JFrame openedWindow;

//inside the listener
if(openedWindow)
   openedWindow.dispose();
else dispose();

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

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