gpt4 book ai didi

java - 切换浏览器标签时隐藏小程序的JDialog

转载 作者:行者123 更新时间:2023-11-29 18:24:14 25 4
gpt4 key购买 nike

我的问题似乎与How to hide JDialog from JApplet when user switch browser tab? 非常相关但它没有提供有效的解决方案。

我开发了一个使用 JWS/JNLP 部署的小程序。它首先显示“新游戏”对话框。

private class NewGameDialog extends CustomDialog implements ActionListener {
...
public NewGameDialog () {
super(topContainer, "NEW GAME", modalityType);

System.out.println(topContainer + " " + modalityType);
//prints JApplet and DOCUMENT_MODAL
...

CustomDialog 只是扩展了 JDialog:

public class CustomDialog extends JDialog implements WindowListener {

public CustomDialog(Container cont, String title, ModalityType modalityType) {
super(windowForComponent(cont), title, modalityType);
}

public static Window windowForComponent(Component c) {
if (c instanceof Window) return (Window) c;
return SwingUtilities.windowForComponent(c);
}

...

这是从 GUI 类调用 JDialog 的方式:

public void requestNewGame () {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
NewGameDialog dialog = new NewGameDialog();
dialog.setVisible(true);
}
});
}

enter image description here

我使用了 How to Use Modality in Dialogs 中描述的不同类型的模式.目标是在用户切换到浏览器中的另一个选项卡时隐藏 JDialog。但是似乎没有一个选项不起作用。该对话框始终 float 在所有选项卡上方。

请指教如何在用户移动到另一个选项卡时隐藏对话框并在用户返回我的小程序选项卡后再次显示?

最佳答案

这是一种推定,即当标签更改时,父组件在屏幕上更改位置。例如

JDialog.getParent().getLocationOnScreen()

经过测试并可用于:

  • 火狐 19.0
  • Chrome 版本 25.0.1364.97 m

失败于:

  • Internet Explorer 8.0.7601.17514

使用 MODELESS 以外的其他 Dialog.ModalityType 值要么拒绝访问浏览器选项卡,要么导致安全异常。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DialogApplet extends JApplet {

@Override
public void init() {
Runnable r = new Runnable() {
@Override
public void run() {
initGUI();
}
};
SwingUtilities.invokeLater(r);
}

public void initGUI() {
JButton b = new JButton("Show Dialog");
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
showDialog();
}
};
b.addActionListener(listener);
add(b);
}

private JDialog d;
JTextArea output;
int currentX = -1;
Timer timer;

public void showDialog() {
if (d==null) {
output = new JTextArea(5,20);
Window w = SwingUtilities.windowForComponent(this);
d = new JDialog(w, "Dialog", Dialog.ModalityType.MODELESS);
//Dialog.ModalityType.TOOLKIT_MODAL); //security
//Dialog.ModalityType.DOCUMENT_MODAL);
//Dialog.ModalityType.APPLICATION_MODAL);
d.add(output, BorderLayout.CENTER);
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
StringBuilder sb = new StringBuilder();


sb.append("parent location: \t" +
d.getParent().getLocation() + "\n");
sb.append("parent location - screen: \t" +
d.getParent().getLocationOnScreen() + "\n");

System.out.print(sb.toString());

output.setText(sb.toString());
if (currentX>-1 &&
currentX!=d.getParent().getLocationOnScreen().getX()
) {
System.out.println( "Goodbye world!" );
d.setVisible(false);
timer.stop();
}
}
};
timer = new Timer(1000, al);
d.pack();
d.setLocationRelativeTo(d.getParent());
}
currentX = (int)d.getParent().getLocationOnScreen().getX();
timer.start();
d.setVisible(true);
}
}

Java 脚本

或许可以改用 JS 来解决这个问题。诀窍似乎在于检测 HTML 窗口焦点/模糊事件。例如。详见答案:

很明显,当 JS 检测到选项卡更改时,我们会让它调用小程序中的方法,例如 tabVisible()tabHidden() 来对对话框。

关于java - 切换浏览器标签时隐藏小程序的JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15183146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com