【问题标题】:Function to navigate to a web page in Java在 Java 中导航到网页的函数
【发布时间】:2014-04-11 11:01:48
【问题描述】:

我正在尝试制作一个允许您导航到网页的功能。我可以如何运行该功能,我只是不知道如何编写访问网页的程序部分。这是我用来通过 JButton 访问函数的代码。我希望该程序可以在多个平台上运行。我找到的所有解决方案,要么我理解得不够好,无法根据我的需要进行修改,要么它不是多平台的。

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton google = new JButton("Google");
        linux.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        openURL("http://www.google.com/"); 
                }
        } );
        JButton stackoverflow = new JButton("Stackoverflow");
        JButton blah = new JButton("blah");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.add(linux);
        panel.add(osx);
        panel.add(windows);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }

是的,我知道最后两个按钮什么也没做。

这是我迄今为止尝试过的:

    public static void openURL(String url) {
        String osName = System.getProperty("os.name");
        try {
            if (osName.startsWith("Windows"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            else {
                String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape", "chrome" };
                String browser = null;
                for (int count = 0; count < browsers.length && browser == null; count++)
                    if (Runtime.getRuntime().exec(new String[] {"which", browsers[count]}).waitFor() == 0)
                    browser = browsers[count];
                Runtime.getRuntime().exec(new String[] {browser, url});
        }
    }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage());
        }
    }

不幸的是,我不明白这是做什么的,或者如何根据我的需要进行更改。

如果可能,您能否解释一下您的解决方案,以便我了解它的工作原理?谢谢。

【问题讨论】:

标签: java swing jbutton


【解决方案1】:

您可以使用 Desktop 类,它允许 Java 应用程序与主机平台上与特定文件类型关联的默认应用程序进行交互。这里有一个关于How to integrate with the Desktop class 的教程。

记住:

使用isDesktopSupported()方法判断桌面是否 API 可用

我做了一个简单的例子。

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DesktopTest {

    private JPanel panel;

    public DesktopTest() {
        panel = new JPanel();

        ActionListener listener = new OpenUrLAction();

        JButton googleButton = new JButton("google");
        googleButton.setActionCommand("http://www.google.com");
        googleButton.addActionListener(listener);

        JButton stackOverButton = new JButton("stackOverflow");
        stackOverButton.setActionCommand("http://www.stackoverflow.com");
        stackOverButton.addActionListener(listener);

        panel.add(googleButton);
        panel.add(stackOverButton);

    }

    public JPanel getPanel() {
        return panel;
    }

    private class OpenUrLAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                                        Desktop desktop = Desktop.getDesktop();
                    desktop.browse(new URI(e.getActionCommand()));
                } catch (IOException | URISyntaxException e1) {
                    JOptionPane.showMessageDialog(null,
                            "An error happen " + e1.getMessage());
                }
            }

        }

    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("DesktopExample");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setLocationByPlatform(Boolean.TRUE);
        frame.add(new DesktopTest().getPanel());

        // Display the window.
        frame.pack();
        frame.setVisible(Boolean.TRUE);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

如果你觉得这没什么用,你可以在answer找到一些解决方法

【讨论】:

    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2014-09-06
    • 2021-07-03
    • 2015-09-17
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多