java - JLabel 更改时 Applet 重新定位

标签 java swing applet

我目前有一个 JFrame,其中包含一个 Applet 和一个 JTabbedPane。每当选项卡式 Pane 上的 JLabel 更新时,小程序就会向下移动一个像素,这比您想象的更烦人。我能做些什么来防止这种情况发生吗?

这是我正在处理的示例(对于我的问题,只需将鼠标悬停在“Foo”上)[启动小程序大约需要 10-15 秒,音乐一旦启动就会播放,按钮将禁用它(音符)]:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;

public class ToolkitFrame extends JFrame {

    public void construct() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(getApplet(), BorderLayout.CENTER);
        add(getTabs(), BorderLayout.EAST);
        setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

    public JTabbedPane getTabs() {
        tabs = new JTabbedPane();
        tabs.add(getTab());
        return tabs;
    }

    public JPanel getTab() {
        label = new JLabel("Foo");
        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent event) {
                label.setText("Bar");
            }

            @Override
            public void mouseExited(MouseEvent event) {
                label.setText("Foo");
            }

        });

        JPanel panel = new JPanel();
        panel.add(label);
        return panel;
    }

    public Applet getApplet() {
        Applet applet = null;
        try {
            String url = "http://oldschool59.runescape.com/";
            String source = getPageSource(new URL(url));
            Matcher matcher = Pattern.compile("code=(.*) ").matcher(source);
            if (matcher.find()) {
                LoaderStub stub = new LoaderStub(Pattern.compile("<param name=\"([^\\s]+)\"\\s+value=\"([^>]*)\">"), source);
                String appletClass = matcher.group(1);
                stub.setCodeBase(new URL(url));
                stub.setDocumentBase(new URL(url));
                try {
                    applet = (Applet) new URLClassLoader(new URL[] {
                        new URL(url + "gamepack.jar")
                    }).loadClass(appletClass.replaceAll(".class", "")).newInstance();
                    applet.setBackground(Color.BLACK.darker());
                    applet.setPreferredSize(new Dimension(765, 503));
                    applet.setStub(stub);
                    applet.init();
                    applet.start();
                    applet.requestFocusInWindow();
                    return applet;
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        return applet;
    }

    class LoaderStub implements AppletStub {

        private Map<String, String> parameters = new HashMap<String, String>();

        private URL documentBase;
        private URL codeBase;

        public LoaderStub(Pattern parameterPattern, String frameSource) {
            Matcher param = parameterPattern.matcher(frameSource);
            while (param.find()) {
                String key = param.group(1);
                String value = param.group(2);
                parameters.put(key, value);
            }
        }

        public void setDocumentBase(URL documentBase) {
            this.documentBase = documentBase;
        }

        public void setCodeBase(URL codeBase) {
            this.codeBase = codeBase;
        }

        @Override
        public boolean isActive() {
            return true;
        }

        @Override
        public URL getDocumentBase() {
            return documentBase;
        }

        @Override
        public URL getCodeBase() {
            return codeBase;
        }

        @Override
        public String getParameter(String name) {
            return parameters.get(name);
        }

        @Override
        public AppletContext getAppletContext() {
            return null;
        }

        @Override
        public void appletResize(int width, int height) {

        }

    }

    public static String getPageSource(URL url) throws IOException, InterruptedException {
        URLConnection uc = url.openConnection();
        uc.addRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        uc.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        uc.addRequestProperty("Accept-Encoding", "gzip,deflate");
        uc.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
        uc.addRequestProperty("Connection", "keep-alive");
        uc.addRequestProperty("Host", "www.runescape.com");
        uc.addRequestProperty("Keep-Alive", "300");
        uc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6");
        DataInputStream di = new DataInputStream(uc.getInputStream());
        byte[] tmp = new byte[uc.getContentLength()];
        di.readFully(tmp);
        di.close();
        return new String(tmp);
    }

    private JLabel label;
    private JTabbedPane tabs;

    private static final long serialVersionUID = -6757985823719418526L;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ToolkitFrame().construct();
            }

        });
    }

}

最佳答案

        applet.setLayout(null);
        applet.setBounds(0,0,770,530);

这将强制小程序的位置始终位于 0,0。只需将此代码放入 applet.start() 之前的某个位置即可

关于java - JLabel 更改时 Applet 重新定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346804/

相关文章:

Java计算我的天使不准确?

java - 设置JAVA_HOME

java - 如何使用工具包和新 URL 在 Java 中加载图像

java - 为 Hangman 游戏编译 java 源代码时出错

Java 如何避免字符串索引越界

Java切换jpanels和新面板没有响应

java - JList 未显示在 JScrollPane 上

java - 按下按钮时读取一行

java - 如何在小程序中的不同行添加文本字段?

OS X Mountain Lion 上带有自签名证书的 Java 小程序