简体   繁体   English

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

[英]Java/Javascript bring browser window containing applet to foreground

Is there a way to pull a browser window to the foreground/focus from a Java applet alert window? 有没有办法将浏览器窗口从Java applet警报窗口拉到前景/焦点? I have an applet in a html page that brings up an alert with a button in it. 我在html页面中有一个applet,其中显示了带有按钮的警报。 When the button is pressed, I want the original browser window to pop up from wherever it is (minimized, covered, etc.) I believe there is a way to connect Java to Javascript to do this, but I don't know Javascript. 当按下按钮时,我希望从任何地方(最小化,覆盖等)都弹出原始浏览器窗口,我相信可以通过一种方法将Java连接到Javascript,但是我不知道Javascript。

Here is the Java applet code: 这是Java applet代码:

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

}

And here is the HTML code: 这是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>

(Changed from comment to answer so I can post a decent code snippet for you) There is no reliable way to "foreground" a window using JavaScript, unless it's a window you created yourself from a parent page, for obvious reasons ("Congratulations!! You are our 1000th customer, you win a prize!!!!!"). (从注释改为回答,这样我就可以为您发布一个不错的代码段。)没有可靠的方法使用JavaScript“前景”显示窗口,除非出于明显的原因,它是您从父页面创建的窗口(“恭喜! !您是我们的第1000位客户,您将赢得大奖!!!!”)。 If you've loaded your applet window as a popup using window.open from another window, you could try using window.focus: 如果您已使用applet窗口从另一个窗口使用window.open加载为弹出窗口,则可以尝试使用window.focus:

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

I imagine popup blockers don't appreciate this sort of thing, so YMMV. 我想象弹出窗口阻止程序不喜欢这种事情,所以YMMV。 On a personal note, I would suggest that you have a think about whether a user wants to know about your alert if they've chosen to hide/minimise your app. 就个人而言,我建议您考虑一下用户是否希望了解您的警报(如果他们选择隐藏/最小化您的应用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM