【问题标题】:How to relocate the applet viewer window?如何重新定位小程序查看器窗口?
【发布时间】:2016-07-28 22:32:32
【问题描述】:

使用Eclipse制作java Applet。每次从 IDE 运行它时,applet 查看器都会显示在左上角 (0,0) 处。如何在开发过程中可编程地将其更改为屏幕中间?我知道在浏览器中部署时,我们无法从小程序内部更改窗口,因为 html 确定位置。

【问题讨论】:

    标签: java applet


    【解决方案1】:

    与另一张海报相比,我认为这是一个毫无意义的练习,并且更喜欢他们的建议,即制作一个混合应用程序/小程序以使开发更容易。

    OTOH - “我们拥有技术”。小程序查看器中小程序的顶级容器通常是Window。获取对它的引用,您可以将其设置在您希望的位置。

    试试这个(恼人的)小例子。

    // <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】:

    有趣的问题。

    我还没有找到影响 AppletViewer 的可靠方法,而不是不使用 Windows 上的脚本从批处理文件模式启动它,即使这样也效果不佳。

    另一种方法是编写您的测试代码,以便 Applet 从 JFrame 开始,您可以轻松地将其居中。

    在你的 Applet 中添加一个 main 方法:

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

    这应该可以工作,除非我错过了什么 - 我更新了我在我的机器上运行的工作代码。

    【讨论】:

    • “有趣的问题。”我不同意这一点,但其余的 +1。
    • @AndrewThompson 我明白你的意思了!那里有很棒的技术答案,我不知道有人可以做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2011-06-13
    • 2018-12-14
    • 2019-11-13
    • 2013-02-18
    相关资源
    最近更新 更多