你好社区:), 所以我是一个真正的java新手,对吧?我似乎无法从 LINK GUI 中获取要打开的 URL。目前,它仅显示字段值。
这是我的 handler.java 代码:
google = new JButton("Google");
google.setToolTipText("Open Google");
add(google);
我希望它打开 URL 而不是显示字段值。这是我的 Action 监听器代码:
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
如果可能的话,我可以获得一些帮助吗? :)谢谢
最佳答案
I get an error,
Cannot make a static reference to the non-static method browse(URI) from the type Desktop
不,这就是当您尝试调用类的实例方法时会发生的情况。您需要一个 Desktop
对象。
URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri.toURL());
关于java - 在简单的 Java 程序中从 JButton 打开 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10037644/