2015-05-29 153 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