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

发布于 2024-11-28 05:23:18 字数 3224 浏览 0 评论 0原文

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

I'm new to java. I want to embed a browser in a JFrame. I used a JEditorPane to display the HTML, but it still doesn't display the page correctly. 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. I'm probably not using JEditorPane correctly. 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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2024-12-05 05:23:18

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

  1. lobo java 浏览器
  2. lobo java 浏览器 net/ns/index.html" rel="nofollow">DJ Project Native Swing
  3. 您也可以考虑 SWT 浏览器

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

希望这有帮助。

you should consider a swing java embedded browser. Examples of this are:

  1. lobo java browser
  2. DJ Project Native Swing
  3. you could also condsider SWT Browser

those are all free open source java embeded browsers.

hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文