简体   繁体   English

关闭Swing模态弹出窗口

[英]Closing Swing modal popups

When I am trying to hide or close popup dialog invoked as a modal, the components disappears as they should, but the grey screen that indicates modality of the window is still visible, until the fist mouse click event at this window area. 当我尝试隐藏或关闭作为模式调用的弹出对话框时,这些组件将按原样消失,但是指示该窗口的模态的灰色屏幕仍然可见,直到该窗口区域的第一手鼠标单击事件为止。

WebPopup darkenScreen = new WebPopup(PopupStyle.gray);
ContructPopUP(darkenScreen);
darkenScreen.showPopupAsModal(this);

And popup settings method : 和弹出设置方法:

private void ContructPopUP(WebPopup darkenScreen)
{
    final JFrame mFrame = this;
    final WebTextField inputTime = new WebTextField("(sekundy)");
    darkenScreen.setLayout(new GridLayout(3, 1));
    darkenScreen.add(new WebLabel("Podaj czas : "));
    darkenScreen.add(inputTime);
    darkenScreen.add(new WebButton(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            int secTime = Integer.parseInt(inputTime.getText());
            if (secTime > 0 && secTime < 7200)
            {
                Connection.TurnOff(secTime);
                System.out.println("clicked!");
            }

            darkenScreen.hidePopup();
        }
    }));
}

When invoking as ordinary popup everything disappears as indented. 当像普通的弹出窗口一样调用时,一切都会缩进。 I've tried to close it in many ways but none of them worked. 我尝试了多种方式将其关闭,但均无效果。

Before clicking button and executing popup.hide : 在单击按钮并执行popup.hide之前: 在popup.hide()之前

after doing it : 完成后:

后

Assuming you are using the WebLaF library , I think your problem might be caused by the PopupLayer.hidePopup method. 假设您使用的是WebLaF库 ,我认为您的问题可能是由PopupLayer.hidePopup方法引起的。 This method is called by the WebPopup.hidePopup method and should hide the modal popup, but as you noticed, the gray layer does not disappear. 该方法由WebPopup.hidePopup方法调用,并且应该隐藏模式弹出窗口,但是正如您所注意到的,灰色层不会消失。 If you look at PopupLayer.hideAllPopups , all popups are removed in this method and the popup layer is made invisible . 如果您查看PopupLayer.hideAllPopups ,则在此方法中将删除所有弹出窗口,并且使弹出层不可见 I do not have experience with the WebLaF library and it feels hackish, but you might be able to solve your problem by hiding the popup layer yourself: 我没有使用WebLaF库的经验,感觉有点黑,但是您可以通过自己隐藏弹出层来解决问题:

import com.alee.laf.button.WebButton;
import com.alee.laf.label.WebLabel;
import com.alee.laf.text.WebTextField;
import com.alee.managers.popup.PopupStyle;
import com.alee.managers.popup.WebPopup;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ModalWebPopup {
    public static void main(final String[] arguments) {
        new ModalWebPopup().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow: modal WebPopup");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        final JButton button1 = new JButton("Show a modal WebPopup");
        panel.add(button1);
        frame.getContentPane().add(panel);

        button1.addActionListener(actionEvent -> {
            final WebPopup darkenScreen = new WebPopup(PopupStyle.gray);
            constructPopup(darkenScreen);
            darkenScreen.showPopupAsModal(frame);
        });

        frame.setVisible(true);
    }

    private void constructPopup(final WebPopup darkenScreen) {
        //final JFrame mFrame = this;
        final WebTextField inputTime = new WebTextField("(sekundy)");
        darkenScreen.setLayout(new GridLayout(3, 1));
        darkenScreen.add(new WebLabel("Podaj czas : "));
        darkenScreen.add(inputTime);
        darkenScreen.add(new WebButton(actionEvent -> {
            int secTime = Integer.parseInt(inputTime.getText());
            if (secTime > 0 && secTime < 7200) {
                //Connection.TurnOff(secTime);
                System.out.println("clicked!");
            }

            System.out.print("Hide the modal WebPopup ");

            // Normal way to hide the popup:
            //darkenScreen.hidePopup();

            System.out.println("by making the parent of the WebPopup invisible.");

            // Alternative way to hide the popup:
            darkenScreen.getParent().setVisible(false);

            // Compare the PopupLayer.hideAllPopups and PopupLayer.hidePopup methods
            // for more details.
        }));
    }
}

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

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