【问题标题】:Applet embedded in browser embedded in JFrame嵌入在 JFrame 中的浏览器中嵌入的 Applet
【发布时间】:2011-10-21 07:57:43
【问题描述】:

我是 Java 新手。我想在 JFrame 中嵌入浏览器。我使用 JEditorPane 来显示 HTML,但它仍然无法正确显示页面。它显示了页面,但事情不合适。我最担心的是嵌入在网页中的 Java 小程序没有显示在我的自定义浏览器中。我可能没有正确使用 JEditorPane。这是我的代码:

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

public class Frame1 extends JFrame implements ActionListener
{
    private JEditorPane editorPane;    
    private JTextField addressField;
    private JButton goButton;
    private JLabel label;

    public Frame1() {        
        super("Frame1");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
            System.out.println("Unablet to set look and feel.");
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label = new JLabel("Enter URL. Must include http://");

        // setup editor pane
        editorPane = new JEditorPane();
        editorPane.setEditable(false);
        URL url = null;
        try {
            url = new URL("http://www.cut-the-knot.org/SimpleGames/FrogsAndToads2d.shtml");
        } catch (MalformedURLException e) {

        }

        if (url != null) {
            try {
                editorPane.setPage(url);
            } catch (IOException e) {
                label.setText("Attempted to read a bad URL: " + url);
            }
        } else {
            label.setText("String specifies an unknown protocol.");
        }

        // put editor pane in a scroll pane
        JScrollPane editorScrollPane = new JScrollPane(editorPane);
        editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(250, 145));
        editorScrollPane.setMinimumSize(new Dimension(10, 10));

        // setup everything in southPanel.
        // southPanel > addressBar + label> addressField + goButton
        addressField = new JTextField(30);
        goButton = new JButton("Go");    
        goButton.addActionListener(this);
        JPanel addressBar = new JPanel();           
        JPanel southPanel = new JPanel(new GridLayout(2, 1));
        addressBar.add(addressField);
        addressBar.add(goButton);
        southPanel.add(addressBar);
        southPanel.add(label);

        // add everything to JFrame
        setLayout(new BorderLayout());        
        add(southPanel, BorderLayout.SOUTH);
        add(editorScrollPane, BorderLayout.CENTER);

        setSize(500, 500);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == goButton) {
            URL url = null;
            try {
                url = new URL(addressField.getText());
            } catch (MalformedURLException murle) {

            }
            if (url != null) {
                try {
                    editorPane.setPage(url);
                } catch (IOException ioe) {
                    label.setText("Attempted to read a bad URL: " + url);
                }
            } else {
                label.setText("String specifies an unknown protocol.");
            }
        }
    }

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

【问题讨论】:

  • JEditorPane 不是浏览器;它是一个文本组件。另请参阅answer。
  • 是的,Swing 的 HTML 支持仅用于格式化,而不用于嵌入小程序或类似内容。您也许可以嵌入appletviewer 的一部分,但是您不会有任何 HTML 输出,只有小程序。
  • 顺便说一句,在 catch 块中没有任何东西的 try/catch 是非常糟糕的形式。不要只是压制异常。

标签: browser applet embed java


【解决方案1】:

您应该考虑使用 Swing Java 嵌入式浏览器。例如:

  1. lobo java browser
  2. DJ Project Native Swing
  3. 你也可以考虑SWT Browser

这些都是免费的开源 Java 嵌入式浏览器。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-09-07
    • 2013-12-08
    • 1970-01-01
    • 2011-01-02
    • 2016-07-22
    • 2022-09-27
    • 2011-11-27
    • 2010-09-22
    • 2015-12-31
    相关资源
    最近更新 更多