gpt4 book ai didi

java - 网页未在我的简单网络浏览器中显示(用 Java 编写)

转载 作者:行者123 更新时间:2023-12-02 05:18:50 25 4
gpt4 key购买 nike

我刚刚制作了一个简单的网络浏览器。但浏览器实际上无法显示任何内容。输入 URL 并按 Enter 键后,什么也没有发生。浏览器窗口保持灰色。哪里错了?

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

}

}

勉类:

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


}

}

最佳答案

您永远不会将 JEditorPane 添加到 JFrame。您只需添加地址栏和一个空的 JScrollPane。

改变

add(new JScrollPane(), BorderLayout.CENTER);

add(new JScrollPane(display), BorderLayout.CENTER);

关于java - 网页未在我的简单网络浏览器中显示(用 Java 编写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com