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>

4

1 に答える 1

1

(コメントから回答に変更されたので、適切なコード スニペットを投稿できます) JavaScript を使用してウィンドウを "フォアグラウンド" にする信頼できる方法はありません。ただし、それが親ページから自分で作成したウィンドウでない限り、明らかな理由 ("おめでとうございます! ! あなたは私たちの 1000 人目のお客様です。賞品を獲得してください!!!!!")。別のウィンドウから window.open を使用してアプレット ウィンドウをポップアップとしてロードした場合は、window.focus を使用してみてください。

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

ポップアップブロッカーはこの種のことを評価しないと思うので、YMMV. 個人的には、ユーザーがアプリを非表示/最小化することを選択した場合に、アラートについて知りたいかどうかを考えることをお勧めします。

于 2015-06-25T12:39:24.840 に答える