2015-05-29 91 views
0

当我试图隐藏或关闭弹出对话框作为模态调用时,组件会自动消失,但指示窗口模态的灰色屏幕仍然可见,直到第一次点击鼠标事件这个窗口区域。关闭Swing模式弹出框

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

和弹出的设置方法:

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(); 
     } 
    })); 
} 

当调用普通弹出一切消失,缩进。我试图用很多方式关闭它,但都没有工作。

前点击按钮,执行popup.hide: Before popup.hide()

这件事以后:

after

+0

邮报[MCVE](http://stackoverflow.com/帮助/ MCVE)。 – user1803551

回答

1

假设你正在使用的WebLaF library,我认为你的问题可能是由PopupLayer.hidePopup方法引起。该方法由WebPopup.hidePopup方法调用,并应隐藏模式弹出窗口,但正如您注意到的那样,灰色层不会消失。如果您查看PopupLayer.hideAllPopups,则在此方法中将删除所有弹出窗口,并且弹出窗口层将变为不可见。我没有经验与WebLaF库和感觉hackish的,但你也许可以通过隐藏弹出层自己来解决问题:

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. 
     })); 
    } 
} 
+0

非常感谢您,WebPopup.getParent()。setVisible(false)完成了工作,现在Popup和灰色图层都被隐藏了。 – baka1408