Java - 创建网络浏览器

标签 java sockets browser websocket

我正在用 java 创建一个 Web 浏览器,当我尝试运行它时收到以下错误:

Exception in thread "main" java.net.MalformedURLException: no protocol:

我无法找到我的特定问题的答案,但我相信这与我的套接字有关。我只需要添加 MalformedURLException 吗?如有任何帮助,我们将不胜感激。

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Browser extends JFrame {

    public JPanel addressPanel, windowPanel;
    public JLabel addressLabel;
    public JTextField textField;
    public JEditorPane windowPane;
    public JScrollPane windowScroll;
    public JButton addressButton;
    private Search search = new Search();

    public Browser() throws IOException {

        addressLabel = new JLabel(" address: ", SwingConstants.CENTER);
        textField = new JTextField("Enter a web address..");
        textField.addActionListener(search);

        addressButton = new JButton("Go");
        addressButton.addActionListener(search);

        windowPane = new JEditorPane("");
        windowPane.setContentType("text/html");
        windowPane.setEditable(false);

        addressPanel = new JPanel(new BorderLayout());
        windowPanel = new JPanel(new BorderLayout());

        addressPanel.add(addressLabel, BorderLayout.WEST);
        addressPanel.add(textField, BorderLayout.CENTER);
        addressPanel.add(addressButton, BorderLayout.EAST);

        windowScroll = new JScrollPane(windowPane);
        windowPanel.add(windowScroll);

        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());

        pane.add(addressPanel, BorderLayout.NORTH);
        pane.add(windowPanel, BorderLayout.CENTER);

        setTitle("Web Browser");
        setSize(1000, 1000);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public class Search implements ActionListener {

        public void actionPerformed(ActionEvent ea) {

            String line;
            try {

                Socket socket = new Socket(textField.getText(), 80);
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out.print("GET / HTTP/1.1\r\n");
                out.print(textField.getText() + "\r\n\r\n");
                out.flush();
                while ((line = in.readLine()) != null) {

                    System.out.println(line);
                }
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {

        Browser browser = new Browser();
    }
}

最佳答案

问题是因为以下行:

windowPane = new JEditorPane("");

只需更改为

windowPane = new JEditorPane();


根据JEditorPane构造函数javadoc:

Creates a JEditorPane based on a string containing a URL specification.
@param url the URL
@exception IOException if the URL is null or cannot be accessed

关于Java - 创建网络浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208752/

相关文章:

java - GridBagLayout:如何防止不可见组件折叠

java - Location.convert() 是否生成以逗号作为分隔符的值?

javascript - Firefox 中的 onbeforeunload

python - 如何使用tornado同时作为套接字服务器和Web服务器?

javascript - Node.js + Socket.io 如何从 PHP 接收数据

jquery - 如何知道滚动条是否出现在浏览器中(jQuery)?

javascript - 如何根据当前实际屏幕宽度显示适合特定视口(viewport)宽度的网页

java - Spring安全客户端PKCE与Keycloak

java - 隐藏/更改 JTree 上的折叠 [-] 和展开 [ ] 按钮

Python 的 recvfrom() 函数不返回任何数据