简体   繁体   English

嵌入在JFrame中的浏览器中嵌入的Applet

[英]Applet embedded in browser embedded in JFrame

I'm new to java. 我是Java新手。 I want to embed a browser in a JFrame. 我想在JFrame中嵌入浏览器。 I used a JEditorPane to display the HTML, but it still doesn't display the page correctly. 我使用了JEditorPane来显示HTML,但是它仍然无法正确显示页面。 It shows the page, but things are out of place. 它显示了页面,但情况不正确。 My biggest concern is that Java applets that are embedded in web pages are not showing up in my custom browser. 我最大的担心是,嵌入在网页中的Java小程序没有显示在我的自定义浏览器中。 I'm probably not using JEditorPane correctly. 我可能没有正确使用JEditorPane。 Here is my code: 这是我的代码:

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();
    }
}

you should consider a swing java embedded browser. 您应该考虑使用摆动式Java嵌入式浏览器。 Examples of this are: 例如:

  1. lobo java browser lobo Java浏览器
  2. DJ Project Native Swing DJ Project本机摇摆
  3. you could also condsider SWT Browser 您也可以考虑SWT浏览器

those are all free open source java embeded browsers. 这些都是免费的开源Java嵌入式浏览器。

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM