首页 > 解决方案 > HTML 页面在 JEditorPane 中未正确显示

问题描述

我有一个 Spring Boot 应用程序,它JFrame在触发事件时显示。框架显示 HTML 页面,但我在这里面临的问题是,当我手动打开 HTML 页面时,它在 chrome 浏览器中正确显示,如下所示:

浏览器视图

但是当我试图通过我的应用程序在框架中打开同一页面时,它看起来像这样:

框架视图

我已经尝试过,但无法弄清楚问题所在。下面是显示框架的代码:

public class BringUpFrame {

    static Logger logger = LoggerFactory.getLogger(BringUpFrame.class);
    private static final int SWP_NOSIZE = 0x0001;
    private static final int SWP_NOMOVE = 0x0002;
    private static final int TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
    private static final HWND HWND_TOPMOST = new HWND(Pointer.createConstant(-1));
    private static JFrame frmOpt;
    static String Message;

    public static JFrame ShowBanner() throws IOException {

        int width = 0;
        int height = 0;
        String ScrnResol = getScreenResolution();
        String[] ScreenResolution = ScrnResol.split(",");

        try {
            width = Integer.parseInt(ScreenResolution[0]);
            height = Integer.parseInt(ScreenResolution[0]);

        } catch (NumberFormatException nfe) {
            logger.info("NumberFormatException: " + nfe.getMessage());
        }

        HWND windowHandle = User32Ext.INSTANCE.GetForegroundWindow();
        frmOpt = new JFrame("Banner");
        frmOpt.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frmOpt.setPreferredSize(new Dimension(width, height));
        frmOpt.setUndecorated(true);
        frmOpt.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        frmOpt.setVisible(true);
        frmOpt.setAlwaysOnTop(false);
        frmOpt.setLocationRelativeTo(null);
        bringToForeground(getHWND(frmOpt), windowHandle);
        JPanel panel = new JPanel();

        panel.setLayout(new BorderLayout());

        JEditorPane jEditorPane = new JEditorPane();
        jEditorPane.setEditable(false);
        URL url = BringUpFrame.class.getClassLoader().getResource("test.htm");

        try {
            jEditorPane.setPage(url);
        } catch (IOException e) {
            jEditorPane.setContentType("text/html");
            jEditorPane.setText("<html>Page not found.</html>");
        }

        JScrollPane jScrollPane = new JScrollPane(jEditorPane);
        jScrollPane.setPreferredSize(new Dimension(width, height));
        panel.add(jScrollPane);

        frmOpt.add(panel);
        frmOpt.pack();
        frmOpt.setVisible(true);

        return frmOpt;

    }

    public static void disposeBanner() {
        frmOpt.dispose();
    }

    private static void bringToForeground(HWND clientWindow, HWND tillHandle) {
        User32Ext.INSTANCE.SetWindowPos(clientWindow, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
    }

    private static HWND getHWND(Component comp) {
        return new HWND(Native.getComponentPointer(comp));
    }

    public static String getScreenResolution() {

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();

        String resolution = width + "," + height;
        logger.info("Till Resolution is (width,height) : " + resolution);
        return resolution;

    }

}

测试.htm:

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
    width: 100%;
    height: 76px;
    font-size: 72px;
    font-weight: bold;
    font-stretch: normal;
    font-style: normal;
    line-height: 1.00;
    letter-spacing: normal;
    text-align: center;
    color: #333333;
}

.Message-Line {
    width: 50%;
    height: 6px;
    background-color: #cc3333;
    text-align: center;
    margin-left:25%;
    margin-right:25%;
}

h2 {
    width: 100%;
    height: 32px;
    font-size: 28px;
    font-weight: normal;
    font-stretch: normal;
    font-style: normal;
    line-height: 1.14;
    letter-spacing: normal;
    text-align: center;
    color: #333333;
}

.contact {
    width: 100%;
    height: 28px;
    font-size: 24px;
    font-weight: normal;
    font-stretch: normal;
    font-style: normal;
    line-height: 1.17;
    letter-spacing: normal;
    text-align: center;
    color: #333333;
}

.footer {
    width: 745px;
    height: 140px;
    border-radius: 8px;
    background-color: #fbefef;
    margin-top:15%;
    margin-left:25%;
    margin-right:25%;

}
</style>
</head>
<body>

    <h1>Dashboard-unavailable</h1>
    <p class="Message-Line"></p>
    <h2>Please use another dashboard instead</h2>
    <div class="footer">
        <h3 class="contact">
            Please contact the Helpdesk quoting code:<b>{{XYZ}}</b>
        </h3>
        <h3 class="contact">
            dashboard number: <b>{{1234}}</b>
        </h3>
    </div>
</body>

</html>

有人可以帮我吗?

问候, 马尼什

标签: javahtmlcssswingjeditorpane

解决方案


推荐阅读