java 如何将超链接添加到 JFace 对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3968620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:11:38  来源:igfitidea点击:

How can I add a hyperlink to a JFace Dialog

javaswteclipse-rcpjface

提问by Alb

How can I make a hyperlink in a JFace Dialog that when clicked opens the link in the default web browser. A full example would be useful. I know there is a org.eclipse.jface.text.hyperlinkpackage but I can't find a suitable example.

如何在 JFace 对话框中创建超链接,单击该超链接会在默认 Web 浏览器中打开该链接。一个完整的例子会很有用。我知道有一个org.eclipse.jface.text.hyperlink包,但我找不到合适的例子。

回答by tbone

Are you running an RCP application?

您正在运行 RCP 应用程序吗?

If so, then the following code will open your link in the default OS browser:

如果是这样,那么以下代码将在默认操作系统浏览器中打开您的链接:

 // 'parent' is assumed to be an SWT composite
 Link link = new Link(parent, SWT.NONE);
    String message = "This is a link to <a href=\"www.google.com\">Google</a>";
    link.setText(message);
    link.setSize(400, 100);
    link.addSelectionListener(new SelectionAdapter(){
        @Override
        public void widgetSelected(SelectionEvent e) {
               System.out.println("You have selected: "+e.text);
               try {
                //  Open default external browser 
                PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(e.text));
              } 
             catch (PartInitException ex) {
                // TODO Auto-generated catch block
                 ex.printStackTrace();
            } 
            catch (MalformedURLException ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            }
        }
    });

The above assumes that you do not want to scan existing text for hyperlinks but simply wish to create one programmatically. If the former is required then you'll need to use the API from JFace text packages or suchlike.

以上假设您不想扫描现有文本以查找超链接,而只是希望以编程方式创建一个。如果需要前者,那么您将需要使用来自 JFace 文本包等的 API。