How to relocate the applet viewer window?

馋奶兔 提交于 2020-01-22 18:52:30

问题


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.


回答1:


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 - '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();
        // change 2000 (every 2 secs.) to 200 (5 times a second) for REALLY irritating!
        Timer timer = new Timer(2000, 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);
        }
    }
}



回答2:


Interesting question.

I've not found a reliable way to influence AppletViewer, not without using a script on Windows to start it from a batch file mode, and even that didn't work too well.

The alternative is to write your test code so the Applet starts in a JFrame, which you can easily center.

Add a main method to your Applet :

 public class TheApplet extends JApplet {

   int width, height;

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
   }

   public void paint( Graphics g ) {
      g.setColor( Color.orange );
      for ( int i = 0; i < 10; ++i ) {
         g.drawLine( width / 2, height / 2, i * width / 10, 0 );
      }
   }

    public static void main(String args[]) {
        TheApplet applet = new TheApplet();

        JFrame frame = new JFrame("Your Test Applet");
        frame.getContentPane().add(applet);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,480);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        applet.init();

    }
}

This should work, unless I missed something - I updated it work code I have running on my machine.



来源:https://stackoverflow.com/questions/10600712/how-to-relocate-the-applet-viewer-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!