我正在尝试使用eclipse Luna 4.4.1创建一个简单的Java浏览器 我设法创建浏览器,但我有2个问题。
这是我的代码
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
class EditorPaneFrame extends JFrame
{
private JTextField url;
private JButton backButton;
private JEditorPane editorPane;
private Stack urlStack = new Stack();
public EditorPaneFrame()
{
setTitle("Java Web Browser");
setSize(1000,800);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
// set up text field and load button for typing in URL
url = new JTextField(30);
url.addActionListener(
//action listener for when the user press enter
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
try
{
// remember URL for back button
urlStack.push(url.getText());
editorPane.setPage("http://"+url.getText());
}
catch(Exception e)
{
editorPane.setText("Error: " +e);
}
}
}
);
// set up back button and button action
backButton = new JButton("Back");
backButton.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("http://"+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);
}
}
}
});
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(editorPane), "Center");
JPanel panel = new JPanel();
panel.add(new JLabel("URL http://"));
panel.add(url);
panel.add(backButton);
contentPane.add(panel,"North");
}
}
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 {
@SuppressWarnings("deprecation")
public static void main(String [] args)
{
JFrame frame = new EditorPaneFrame();
frame.show();
}
}
任何帮助将不胜感激
答案 0 :(得分:0)
上次我查看时,JEditorPane远远落后于当前版本的HTML。此外,您可能不得不以某种方式插入Javascript引擎(当我十年前尝试过这个时,这是真的)。您可以查看Apache Rhino的Java Javascript引擎。还有像级联样式表/闪存插件这样的东西,我认为你再次需要引导。
祝你好运!