-1

我正在制作一个简单的 Java Web 浏览器,它确实从 URL 加载网页,但不知何故,它们似乎根本不正确:

在此处输入图像描述

页面上的加号按钮和链接不起作用。我认为这与 HTML 或 JS 有关。如果这对您有用,我正在使用 Eclipse。我错过了什么?

我的代码

代码:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class WebBrowser
{
     public static void main(String [] args)
     {
          JFrame frame = new EditorPaneFrame();
          frame.show();
     }
}
class EditorPaneFrame extends JFrame
{
     private JTextField url;
     private JCheckBox editable;
     private JButton Carica;
     private JButton Indietro;
     private JEditorPane editorPane;
     private Stack urlStack = new Stack();
     public EditorPaneFrame()
     {
          setTitle("Java Web Browser");
          setSize(600,400);
          setLocationRelativeTo(null);
          setVisible(true);
          // set up text field and load button for typing in URL
          String protocollo = new String ("http://");
          url = new JTextField(protocollo,30);
          Carica = new JButton("Load");
          Carica.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent event)
               {
                    try
                    {
                         // remember URL for back button
                         urlStack.push(url.getText());
                         editorPane.setPage(url.getText());
                    }
                    catch(Exception e)
                    {
                         editorPane.setText("Error: " +e);
                    }
               }
          });
          // set up back button and button action
          Indietro = new JButton("Back");
          Indietro.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent event)
               {
                    if(urlStack.size()<=1) return;
                    try
                    {
                         urlStack.pop();
                         String urlString = (String)urlStack.peek();
                         url.setText(urlString);
                         editorPane.setPage(urlString);
                    }
                    catch(IOException e)
                    {
                         editorPane.setText("Error : " +e);
                    }
               }
          });
          editorPane = new JEditorPane();
          editorPane.setEditable(false);
          editorPane.addHyperlinkListener(new HyperlinkListener()
          {
               public void hyperlinkUpdate(HyperlinkEvent event)
               {
                    if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                    {
                         try
                         {
                              urlStack.push(event.getURL().toString());
                              url.setText(event.getURL().toString());
                              editorPane.setPage(event.getURL());
                         }
                         catch(IOException e)
                         {
                              editorPane.setText("Error: " + e);
                         }
                    }
               }
          });
          editable = new JCheckBox();
          editable.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent event)
               {
                    editorPane.setEditable(editable.isSelected());
               }
          });
          Container contentPane = getContentPane();
                        contentPane.add(new JScrollPane(editorPane), "Center");
                        JPanel panel = new JPanel();
                        panel.add(Indietro);
                        panel.add(new JLabel("URL"));
                        panel.add(url);
                        panel.add(Carica);
                        contentPane.add(panel,"North");
     }
}
4

2 回答 2

1

您正在使用JEditorPanesojavascript而其他scripts将不起作用。您可以使用JWebPanewhich 在内部使用webkit.

于 2013-06-16T13:44:38.597 回答
0

问题是 java 在 JEditorPanes 中用于 HTML 内容的渲染引擎有一段时间没有更新,因此无法正确渲染您的页面。根据支持 Javascript 和 CSS 的 JEditorPane仅支持 HTML 3.2 和一组初始 CSS,因此 JEditorPane 不支持 HTML 5。

看看以下问题及其答案:

如其他答案所述,您可以查看 Java FX 组件或http://lobobrowser.org/java-browser.jsp等替代方案。

于 2013-06-16T13:41:32.800 回答