关闭 Swing 模式弹出窗口
Closing Swing modal popups
当我试图隐藏或关闭作为模态调用的弹出对话框时,组件会按预期消失,但指示 window 模态的灰色屏幕仍然可见,直到第一次单击鼠标window 地区的活动。
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();
}
}));
}
当作为普通弹出窗口调用时,一切都以缩进形式消失。我尝试了很多方法来关闭它,但 none 方法奏效了。
在单击按钮并执行 popup.hide 之前:
完成后:
假设您使用的是 WebLaF library, I think your problem might be caused by the PopupLayer.hidePopup
method. 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. If you look at PopupLayer.hideAllPopups
,所有弹出窗口都将在此方法中删除,并且弹出层不可见。我没有使用 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.
}));
}
}
当我试图隐藏或关闭作为模态调用的弹出对话框时,组件会按预期消失,但指示 window 模态的灰色屏幕仍然可见,直到第一次单击鼠标window 地区的活动。
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();
}
}));
}
当作为普通弹出窗口调用时,一切都以缩进形式消失。我尝试了很多方法来关闭它,但 none 方法奏效了。
在单击按钮并执行 popup.hide 之前:
完成后:
假设您使用的是 WebLaF library, I think your problem might be caused by the PopupLayer.hidePopup
method. 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. If you look at PopupLayer.hideAllPopups
,所有弹出窗口都将在此方法中删除,并且弹出层不可见。我没有使用 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.
}));
}
}