无法打开网站

时间:2014-10-29 14:23:27

标签: java html swing

我正在准备一个计算机展览项目,我正在为它准备网页浏览器。 它运行完美,但是当我打开google这样的网站时,它显然不清晰。将出现一个蓝色屏幕,其中包含Google徽标和一些文字。

我的代码是:

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.*;

public class browser
extends JFrame
implements HyperlinkListener{
    URL pageURL;
    private static final long serialVersionUID = 1630568393141656837L;
    JPanel panel;
    JButton back,forward,go;
    JTextField website;
    JEditorPane display;
    JScrollPane sp;
public void setPanel()
{
    panel=new JPanel();
    back=new JButton("<<");
    forward=new JButton(">>");
    go=new JButton("GO");
    panel.add(back);
    panel.add(forward);
    website=new JTextField("http://",10);
    panel.add(website);
    panel.add(go);
    sp=new JScrollPane();

    display=new JEditorPane();
    display.setEditable(false);
    display.setContentType("text/html");
    sp.add(display);
    go.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            browse();
        }

    });
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    add(panel,BorderLayout.NORTH);
    add(display,BorderLayout.CENTER);
    setSize(900,500);
    setVisible(true);
}
protected void browse() {
    String page=website.getText();
    try {
        pageURL=new URL(page);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        display.setPage(pageURL);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void main(String arg[])
{
    browser br=new browser();
    br.setPanel();
}
@Override
public void hyperlinkUpdate(HyperlinkEvent arg0) {
    // TODO Auto-generated method stub
    HyperlinkEvent.EventType event=arg0.getEventType();
    if(event==HyperlinkEvent.EventType.ACTIVATED);
    {
        browse();
    }
}
}

1 个答案:

答案 0 :(得分:1)

您无法使用jeditorpane正确查看html页面,因为它有限制。 你只能使用编辑窗格用于基本的html。它不提供良好的CSS支持,它不支持javascript。如果你想正确显示网页,你可以使用一个好的html css和js支持库。看看lobo浏览器{ {3}}您也可以使用http://lobobrowser.org/java-browser.jsp

更新.....

这是一个示例,您可以在swing应用程序中使用javafx webView来正确查看网页。我只对代码进行了少量更改。

完整代码

import com.sun.javafx.application.PlatformImpl;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class browser extends JFrame implements HyperlinkListener {

    URL pageURL;
    private static final long serialVersionUID = 1630568393141656837L;
    JPanel panel;
    JButton back, forward, go;
    JTextField website;
    JScrollPane sp;
    private JFXPanel display;

    WebView webComponent;

    public void setPanel() {
        panel = new JPanel();
        back = new JButton("<<");
        forward = new JButton(">>");
        go = new JButton("GO");
        panel.add(back);
        panel.add(forward);
        website = new JTextField("http://", 10);
        panel.add(website);
        panel.add(go);

        display = new JFXPanel();
        createScene();

        go.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        browse();
                    }
                });

            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(panel, BorderLayout.NORTH);
        add(display, BorderLayout.CENTER);
        setSize(900, 500);
        setVisible(true);
    }

    private void createScene() {
        PlatformImpl.startup(new Runnable() {
            @Override
            public void run() {

                BorderPane borderPane = new BorderPane();
                webComponent = new WebView();
                borderPane.setCenter(webComponent);
                Scene scene = new Scene(borderPane, 450, 450);
                display.setScene(scene);

            }
        });
    }

    protected void browse() {
        String url = website.getText();
        if (url != null && url.length() > 0) {
            webComponent.getEngine().load(url);
        }
    }

    public static void main(String arg[]) {
        browser br = new browser();
        br.setPanel();
    }

    @Override
    public void hyperlinkUpdate(HyperlinkEvent arg0) {
        HyperlinkEvent.EventType event = arg0.getEventType();
        if (event == HyperlinkEvent.EventType.ACTIVATED);
        {
            browse();
        }
    }
}

它看起来像......

enter image description here