首页 > 解决方案 > 有什么方法可以从 JEditorPane 上显示的网站获取响应?

问题描述

我想从独立应用程序进行身份验证。因此,我将我公司的实习生登录页面显示在JEditorPane. 首先,您会看到一个像常见的登录窗口。您输入用户名和密码。如果登录成功,页面将变为“登录成功”。我如何区分这些窗口并获取第二个窗口的 URL?

JEditorPane loginPane = new JEditorPane();
loginPane.setEditable(false);

try {
    loginPane.setPage("http://mypage.blalala.com");
} catch (IOException e) {
    loginPane.setContentType("text/html");
    loginPane.setText("<html>Could not load</html>");
}

JScrollPane scrollPane = new JScrollPane(loginPane);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800, 600));
f.setVisible(true);

标签: javaswingjeditorpane

解决方案


我认为您需要听众,这是我的示例应用程序

/*
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package javaapplication5;

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class DemoAppWebSwing extends JFrame {

    private static final DemoAppWebSwing SINGLETON = new DemoAppWebSwing();

    public void init() {
        JEditorPane loginPane = new JEditorPane();
        loginPane.setEditable(false);

        try {
            loginPane.setPage("https://github.com/vincenzopalazzo");
            loginPane.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        JEditorPane pane = (JEditorPane) e.getSource();
                        if (e instanceof HTMLFrameHyperlinkEvent) {
                            HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                            HTMLDocument doc = (HTMLDocument) pane.getDocument();
                            doc.processHTMLFrameHyperlinkEvent(evt);
                        } else {
                            try {
                                pane.setPage(e.getURL());
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                }
            });
        } catch (IOException e) {
            loginPane.setContentType("text/html");
            loginPane.setText("<html>Could not load</html>");
        }

        JScrollPane scrollPane = new JScrollPane(loginPane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(scrollPane);
        this.setPreferredSize(new Dimension(800, 600));
        this.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SINGLETON.init();
            }
        });
    }

}

使用 HyperlinkListener,您可以在单击链接时获得另一个页面 这是我的侦听器实现:

loginPane.setPage("https://jsp-password-checking-unibas.herokuapp.com");
        loginPane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    JEditorPane pane = (JEditorPane) e.getSource();
                    if (e instanceof HTMLFrameHyperlinkEvent) {
                        HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                        HTMLDocument doc = (HTMLDocument) pane.getDocument();
                        doc.processHTMLFrameHyperlinkEvent(evt);
                    } else {
                        try {
                            pane.setPage(e.getURL());
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                }
            }
        })

在此答案的评论部分与另一位用户讨论时,我们得出的结论是JEditorPane具有一定的局限性。但是,我寻找了另一种将网页导入基于 Java 的 UI 的解决方案。您可以使用JavaFX.

检查来自oracle和/或这篇文章的文档链接以供参考。

这是一个基本示例,JavaFX它与前面显示的示例基本相同,使用Swing.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class LoginScreen extends Application {

    @Override
    public void start(final Stage stage) {

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.load("https://jsp-password-checking-unibas.herokuapp.com");

        Scene scene = new Scene(browser, 800, 600);

        stage.setTitle("Login Example");
        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

推荐阅读