【问题标题】:Is it possibly to create selectable hyperlink with basic Swing components in Java?是否可以使用 Java 中的基本 Swing 组件创建可选择的超链接?
【发布时间】:2011-08-01 19:48:51
【问题描述】:

我正在尝试向 JPanel 添加超链接。我想让它的文本变成蓝色(并带下划线),并且链接应该是可选择的(复制其中的一部分)。所以我尝试使用 JLabel:是的,它允许像这样写一些 [可怕的]:

someLabel.setText("<html><font color=\"#0000ff\"><u>http://example.com</u></font></html>");

但不幸的是,JLabel 不允许选择任何文本。我也尝试使用 JTextField,但相反,它不允许在其字段中使用 HTML/CSS。

那么,是否有任何方法可以使用基本的 Swing 组件创建超链接(带有适当的指示),这将允许选择 [并复制] 其中的一部分,还是我应该尝试使用一些 3rd 方组件?谢谢。

【问题讨论】:

    标签: java html swing jlabel jtextfield


    【解决方案1】:

    您可以在不可编辑的JEditorPane 中显示 HTML 内容。它是可选的,您可以通过HyperlinkListener 使链接起作用:

        JEditorPane content = new JEditorPane();
        content.setContentType("text/html");
        content.setEditable(false);
        content.setText("<html><a href=\"http://stackoverflow.com\">Link</a></html>"));
        content.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    try {
                        Desktop.getDesktop().browse(e.getURL().toURI());
                    } catch (Exception e1) {
                        Logger.getLogger(getClass()).error(
                                "Error opening link " + e.getURL(), e1);
                    }
                }
            }
        });
    

    【讨论】:

      【解决方案2】:

      在这里你如何创建一个带有超链接的 JLabel,然后你可以将它添加到你的 Jpanel:

      public HyperLinkLabel()  
      {  
      JPanel p = new JPanel();  
      final String strURL = "http://www.yahoo.com";  
      final JLabel label = new JLabel("<html><a href=\" " + strURL + "\"> click </a></html>");  
      
      final JEditorPane htmlPane = new JEditorPane();  
      
      
      p.add(label);  
      
      getContentPane().add(BorderLayout.NORTH, p);  
      getContentPane().add(BorderLayout.CENTER, new JScrollPane(htmlPane));  
      setBounds(20,200, 500,500);  
      
      label.addMouseListener(new MouseAdapter() {  
         public void mouseEntered(MouseEvent me) {  
            label.setCursor(new Cursor(Cursor.HAND_CURSOR));  
         }  
         public void mouseExited(MouseEvent me) {  
            label.setCursor(Cursor.getDefaultCursor());  
         }  
         public void mouseClicked(MouseEvent me)  
         {  
            System.out.println("Clicked on Label...");  
            try {  
                 htmlPane.setPage(new URL(strURL));  
              }  
              catch(Exception e) {  
                 System.out.println(e);  
              }  
         }  
        });  
      

      【讨论】:

      • 您的示例的问题在于它不是键盘可聚焦的。虽然这样的功能很容易添加。 1)设置JLabel焦点。 2) 添加一个FocusListener 来改变文本颜色(即setForeground(Color))获得/失去焦点。在MouseListener 中更改Color 可能也是一个好主意。
      【解决方案3】:

      您必须创建一个自定义Jlabel [扩展Jlabel] 并为JLabel 编写一个MouseListener。 当用户单击自定义JLabel 时,您的鼠标侦听器必须完成将用户引导至链接的工作。您要查找的鼠标事件[基本上是MouseListener接口的方法,您必须在其中编写重定向代码]是mouseClicked

      【讨论】:

      • 我的解决方案不正确吗?我给出了上面创建自定义组件的解决方案,认为他会在很多地方使用这个组件。将类中的代码作为单独的组件有助于代码的重用。
      • 该评论似乎与我所指的“可聚焦”无关。 (不,您的解决方案是“正确的”,但最好稍微调整一下。)
      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      相关资源
      最近更新 更多