gpt4 book ai didi

java - 使用 JFrame 的浏览器

转载 作者:行者123 更新时间:2023-12-01 12:15:29 25 4
gpt4 key购买 nike

我正在尝试仅使用 swing 和 awt 创建一个网络浏览器。我也使用过actionlistener和actionevent。但它不起作用。请在这件事上给予我帮助。它运行时没有任何错误,但不加载网页。

import java.awt.*;
import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class img extends JFrame
{
private TextField field=new TextField();
private JButton b=new JButton("go");
private JEditorPane display=new JEditorPane();
private JScrollPane panee=new JScrollPane(display);

public static void main(String args[])
{
img file=new img();
file.frameHandler();
}

public void frameHandler() {
setTitle("Browser");
setSize(1200,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
setResizable(false);
setLocationRelativeTo(null);
addComponentsToFrame(getContentPane());
}

public void addComponentsToFrame(Container pane) {
Insets insets=getInsets();
pane.add(field);
pane.add(panee);
Font font=new Font("STENCIL",Font.ITALIC,10);
field.setFont(font);
field.setBounds(8-insets.left, 30-insets.top,1160, 20);
b.setBounds(1150-insets.left, 30-insets.top,80, 20);
panee.setBounds(8-insets.left, 52-insets.top, 1200, 830);
pane.add(b);

}

private void actionListenerCalls()
{
b.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
loadData("http://"+e.getActionCommand());
}
});

display.addHyperlinkListener(new HyperlinkListener(){

@Override
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadData(e.getURL().toString());
}

}
});
}
private void loadData(String text)
{
try{
display.setPage(text);
}
catch(Exception ee)
{
System.out.println("error");
}
}

}

最佳答案

您尚未调用方法actionListenerCalls来绑定(bind)监听器

可能把它放在这里:

public void frameHandler() {
....
addComponentsToFrame(getContentPane());
actionListenerCalls();//<-- invoke here
setVisible(true);
}

附注请避免使用空布局。

编辑

来自docs:

We don't support the full CSS spec. Refer to the javadoc of the CSS class to see what properties we support. The two major CSS parsing related concepts we do not currently support are pseudo selectors, such as A:link { color: red }, and the important modifier.

Note: This implementation is currently incomplete. It can be replaced with alternative implementations that are complete. Future versions of this class will provide better CSS support.

您可以尝试load external css:

StyleSheet ss = new StyleSheet();
ss.importStyleSheet(styleSheetURL);
HTMLEditorKit kit = (HTMLEditorKit)jEditorPane.getEditorKit();
kit.setStyleSheet(ss);

打开默认浏览器

您可以尝试使用Desktop browse()

来自docs

The browse(uri) method can throw a variety of exceptions, including a NullPointerException if the URI is null, and an UnsupportedOperationException if the BROWSE action is unsupported. This method can throw an IOException if the default browser or application cannot be found or launched, and a SecurityException if a security manager denies the invocation.

private void onLaunchBrowser(ActionEvent evt) {
URI uri = null;
try {
uri = new URI(txtBrowserURI.getText());
desktop.browse(uri);
} catch(IOException ioe) {
System.out.println("The system cannot find the " + uri +
" file specified");
//ioe.printStackTrace();
} catch(URISyntaxException use) {
System.out.println("Illegal character in path");
//use.printStackTrace();
}
}

关于java - 使用 JFrame 的浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27038057/

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