简体   繁体   English

关闭在另一个 JFrame 中打开的 JFrame

[英]Closing a JFrame Opened in Another JFrame

I am building an application which I need to open a JFrame in another JFrame while the first JFrame is disabled.我正在构建一个应用程序,当第一个 JFrame 被禁用时,我需要在另一个 JFrame 中打开一个 JFrame。

The problem is when I want to close the second JFrame I need to enable the first JFrame.问题是当我想关闭第二个 JFrame 时,我需要启用第一个 JFrame。

I have tried in several ways, but it is not working properly and I was not able to reach one goal in each of them.我尝试了多种方法,但它无法正常工作,而且我无法在每种方法中都达到一个目标。 I need to reach both goals:我需要达到两个目标:

  1. Disabling the first Frame when the second Frame is opened打开第二个框架时禁用第一个框架
  2. Enabling it when the second Frame is closed.当第二个 Frame 关闭时启用它。

Here is part of my code:这是我的代码的一部分:

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    PaymentJFrame pjf = new PaymentJFrame();
    //setEnableValue(false);
    //enableFrame();
    this.setEnabled(false);
    pjf.setVisible(true);
    if (!pjf.isActive()){
        this.setEnabled(true);
    }

} 

This code dose not enable the First Frame at all.此代码根本不启用第一帧。 I have tried to use it in another way by adding an enable when the second Frame is closed but it is not working:我试图通过在第二个框架关闭时添加enable来以另一种方式使用它,但它不起作用:

    //Class in first Frame
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    PaymentJFrame pjf = new PaymentJFrame();
    //setEnableValue(false);
    //enableFrame();
    this.setEnabled(false);
    pjf.setVisible(true); 
} 
    //Class in second Frame 
    private void formWindowClosed(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:
    FinancialDocumentsJFrame.setEnableValue(true);
}                                 

Dose any one know how can I reach these goals?有谁知道我怎样才能达到这些目标?

The first Frame is the main frame and the second frame is a class that I make the frame object from it in order to show and get more information from users.第一个框架是主框架,第二个框架是一个类,我从中创建框架对象以显示并从用户那里获取更多信息。

I ma using netBeans IDE designer.我使用 netBeans IDE 设计器。

First of all, a Swing application should only have one JFrame , other windows can be JDialog or somethign else.首先,一个 Swing 应用程序应该只有一个 JFrame ,其他窗口可以是JDialog或其他东西。 As for your question, use this code as an example.至于你的问题,以这段代码为例。 It uses a listener to detect the closing event of the second window.它使用侦听器来检测第二个窗口的关闭事件。 The following code should be in the (first) JFrame (it looks like you have a button there)下面的代码应该在(第一个) JFrame (看起来你有一个按钮)

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          

JDialog paymentDialog = new JDialog();

MyFirstFrame.this.setEnabled(false);
paymentDialog.setVisible(true);

paymentDialog.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        MyFirstFrame.this.setEnabled(true);
    }
});
}

You can create your own dialog by extending JDialog as you did with the frames, and use the custom dialog in this code.您可以像处理框架一样通过扩展JDialog来创建自己的对话框,并在此代码中使用自定义对话框。 Also instead of setting the JFrame enabled or disabled, you could consider using a modal JDialog that blocks actions to the JFrame while the dialog is active.此外,您可以考虑使用模态 JDialog ,而不是将JFrame设置为启用或禁用,它会在对话框处于活动状态时阻止对 JFrame 的操作。

And further still, there is the setAlwaysOnTop(boolean) for Swing windows.此外,还有用于 Swing 窗口的setAlwaysOnTop(boolean)

使用this.dispose()方法JFrame具有用于JFrame关闭的dispose()方法

I decided to use jDialog as it was recommended by many people on the net.我决定使用 jDialog,因为网上很多人都推荐它。

I used a jDialog and copied all the objects I had used in the second jFrame.我使用了一个 jDialog 并复制了我在第二个 jFrame 中使用的所有对象。

It worked the way I wanted it to do.它以我想要的方式工作。

My only problem is that why java hasn't prepared a different way to answer this need.我唯一的问题是,为什么 java 没有准备一种不同的方式来满足这种需求。

I'm using an action listener in the first jFrame to open the second Frame which I have used jDialog for it.我在第一个 jFrame 中使用动作侦听器打开第二个 Frame,我已经使用 jDialog 为它打开了第二个 Frame。

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    ActivityJDialog ajdf = new ActivityJDialog(this,true);
    ajdf.setVisible(true);
} 

And I have copied everything I wanted to have into this jDialog.我已经将我想要的所有内容复制到这个 jDialog 中。

public class ActivityJDialog extends java.awt.Dialog {

//Document Attributes 
int documentStatus = -1;
int documentType = -1;

/**
 * Creates new form AcrivityJDialog
 * @param parent
 * @param modal
 */
public ActivityJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    /*if(false) // Full screen mode
    {
        // Disables decorations for this frame.
        //this.setUndecorated(true);
        // Puts the frame to full screen.
        this.setExtendedState(this.MAXIMIZED_BOTH);
    }
    else // Window mode
    {*/
        // Size of the frame.
        this.setSize(1000, 700);
        // Puts frame to center of the screen.
        this.setLocationRelativeTo(null);
        // So that frame cannot be resizable by the user.
        this.setResizable(false);
    //}
}

Thank you all for helping.谢谢大家的帮助。

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

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