如何重新定位小程序查看器窗口? [英] How to relocate the applet viewer window?

查看:216
本文介绍了如何重新定位小程序查看器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Eclipse进行Java小程序。每次从IDE运行它时,小程序查看器(0,0)显示在左上角。如何可编程其更改为屏幕的发展过程中间?我知道,当部署在浏览器中,我们不能在窗口从applet中由于HTML确定位置改变。

using Eclipse to make java Applet. Every time to run it from the IDE, the applet viewer is shown on left top corner at (0,0). How to programmably change it to the middle of screen during the development? I know when deploy in browser, we can't change the window from inside the applet since the html determines the location.

推荐答案

在此相反的其他海报,我觉得这是一个毫无意义的锻炼和preFER他们的建议作出混合应用程序/小程序来简化开发。

In contrast to the other poster, I think this is a pointless exercise and prefer their suggestion to make an hybrid application/applet to make development easier.

OTOH - '我们有技术。在applet浏览器小应用程序的顶层容器通常是窗口是。获取到的引用,在那里你愿意,你可以设置它。

OTOH - 'we have the technology'. The top-level container of an applet in applet viewer is generally a Window. Get a reference to that, and you can set it where you wish.

试试这个(刺激性)小例子。

Try this (irritating) little example.

// <applet code=CantCatchMe width=100 height=100></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class CantCatchMe extends JApplet {

    Window window;
    Dimension screenSize;
    JPanel gui;
    Random r = new Random();

    public void init() {
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                moveAppletViewer();
            }
        };
        gui = new JPanel();
        gui.setBackground(Color.YELLOW);
        add(gui);

        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Timer timer = new Timer(200, al);
        timer.start();
    }

    public void start() {
        Container c = gui.getParent();
        while (c.getParent()!=null) {
            c = c.getParent();
        }
        if (c instanceof Window) {
            window = (Window)c;
        } else {
            System.out.println(c);
        }
    }

    private void moveAppletViewer() {
        if (window!=null) {
            int x = r.nextInt((int)screenSize.getWidth());
            int y = r.nextInt((int)screenSize.getHeight());
            window.setLocation(x,y);
        }
    }
}

这篇关于如何重新定位小程序查看器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆