Java/Javascript将包含小程序的浏览器窗口置于前台

8
有没有办法从Java小程序的警报窗口将浏览器窗口拉到前台/焦点?我在一个带有警报按钮的html页面中有一个小程序。当按下按钮时,我希望原始浏览器窗口从任何位置(最小化、覆盖等)弹出。我相信有一种方法可以将Java连接到Javascript来实现这一点,但我不知道Javascript。
以下是Java小程序代码:
/** An applet that posts an alert and waits for the alert button to be pressed.
* Version 1 uses http://java.sun.com/products/plugin/1.3/docs/jsobject.html
*/

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

public class Bounce extends JApplet implements ActionListener {
    JDialog dialog;
    JSObject window;
    String message;

    public void paint(Graphics g) {
        g.clearRect(0,0, 400,40);
        g.drawString(message,40,20);
    }

    public void init() {
        JFrame frame= null;
        dialog= new JDialog(frame, "Bounce App");

        JButton setupButton= new JButton("Bounce it back!");
        setupButton.addActionListener(this);

        JPanel contentPane= new JPanel();
        contentPane.add(setupButton);
        contentPane.setOpaque(true);
        dialog.setContentPane(contentPane);

        dialog.setSize(new Dimension(400, 110));
        dialog.setVisible(true);

        message= "This applet posts an alert panel.";
        window= JSObject.getWindow(this);
//      String[] params= { "An alert message" };
//      window.call("alert", params);
//      window.eval("alert('Important Alert!')");
    }

    public void actionPerformed(ActionEvent e) {
        dialog.setVisible(false);
        dialog.dispose();
        System.err.println("button has been pushed; focus set");
        message= "Somebody pushed my bounce-back button."; 
        JSObject document= (JSObject)window.getMember("document");
        document.setMember("bgColor", "orange");
        window.eval("focus()");
        repaint();
    }

}

以下是HTML代码:

    <HTML>
    <HEAD><TITLE>The Reappearing Page</TITLE></HEAD>
    <body bgcolor="#f0ffc0">

    <H2>Make this page reappear</H2>
    This page will start an applet (white box below) that sets up an alert.
    Before you respond to the alert, hide the window you are reading right now,
    using one of these methods:<ul>
    <li> cover it with another window, </li>
    <li> Hide it using a menu item, </li>
    <li> Minimize it, or </li>
    <li> move it to another workspace or desktop. </li>
    </ul>
    Then click on the button in the alert. 

    <P>
    <EMBED type="application/x-java-applet;version=1.3" width="400" height="40" 
        align="baseline" code="Bounce.class" MAYSCRIPT=true
       pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
    <NOEMBED>
       No JDK 1.3 support for APPLET!!
    </NOEMBED>
    </EMBED>

    <P>What is supposed to happen is that the main window 
    will emerge from wherever you hid it and reappear.
    Since I don't know how to do this, it is your challenge to actually make it happen.
    We need to be able to achieve this magic from any of the situations listed above.</P>

    </body>

1个回答

1

(将评论更改为答案,以便我可以为您发布一个不错的代码片段)使用JavaScript没有可靠的方法“置于前台”窗口,除非它是您从父页面创建的窗口,出于显而易见的原因(“恭喜!您是我们的第1000位客户,您赢得了奖品!!!”)。如果您使用window.open从另一个窗口加载了您的小部件窗口作为弹出窗口,则可以尝试使用window.focus:

var w = window.open(appletPageUrl);
w.focus()

我想弹出窗口拦截程序不会欣赏这种事情,所以效果可能因人而异。就个人而言,如果用户选择隐藏/最小化您的应用程序,建议您考虑一下他们是否想知道您的警报。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接