我刚做了一个简单的网络浏览器。但浏览器实际上无法显示任何内容。输入URL并按回车键后,没有任何反应。 broswer窗口保持灰色。 哪里错了?
package readfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ReadFile extends JFrame{
private JTextField addressbar;
private JEditorPane display;
public ReadFile(){
super("My Browser");
addressbar=new JTextField("Enter a hoss");
addressbar.addActionListener(
new ActionListener() {
//@Override
public void actionPerformed(ActionEvent event) {
//throw new UnsupportedOperationException("Not supported yet.");//To change body of generated methods, choose Tools | Templates.
loadCrap(event.getActionCommand());
}
}
);
add(addressbar,BorderLayout.NORTH);
display=new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(
new HyperlinkListener() {
//@Override
public void hyperlinkUpdate(HyperlinkEvent event) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//event.getEventType(event.);
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadCrap(event.getURL().toString());
}
}
}
);
add(new JScrollPane(),BorderLayout.CENTER);
//add(new JScrollPane(),BorderLayout.CENTER);
setSize(500,300);
setVisible(true);
}
private void loadCrap(String userText){
try {
display.setPage(userText);
addressbar.setText(userText);
} catch (Exception e) {
System.out.println("Crap");
}
}
}
Mian class:
package readfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JFrame;
public class ReadFileMain {
public static void main(String[] args) {
ReadFile rf=new ReadFile();
rf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:2)
您永远不会将JEditorPane添加到JFrame。您只是添加地址栏和空JScrollPane。
更改
add(new JScrollPane(), BorderLayout.CENTER);
到
add(new JScrollPane(display), BorderLayout.CENTER);