zoukankan      html  css  js  c++  java
  • How to add hyperlink in JLabel

    You can do this using a JLabel, but an alternative would be to style a JButton. That way, you don't have to worry about accessibility and can just fire events using an ActionListener.

      public static void main(String[] args) throws URISyntaxException {
        final URI uri = new URI("http://java.sun.com");
        class OpenUrlAction implements ActionListener {
          @Override public void actionPerformed(ActionEvent e) {
            open(uri);
          }
        }
        JFrame frame = new JFrame("Links");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(100, 400);
        Container container = frame.getContentPane();
        container.setLayout(new GridBagLayout());
        JButton button = new JButton();
        button.setText("<HTML>Click the <FONT color="#000099"><U>link</U></FONT>"
            + " to go to the Java website.</HTML>");
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setBorderPainted(false);
        button.setOpaque(false);
        button.setBackground(Color.WHITE);
        button.setToolTipText(uri.toString());
        button.addActionListener(new OpenUrlAction());
        container.add(button);
        frame.setVisible(true);
      }
    
      private static void open(URI uri) {
        if (Desktop.isDesktopSupported()) {
          try {
            Desktop.getDesktop().browse(uri);
          } catch (IOException e) { /* TODO: error handling */ }
        } else { /* TODO: error handling */ }
      }
  • 相关阅读:
    js事件循环机制event-loop
    javascript编译与执行
    css中rem,em,px的区别和使用场景
    float
    flex布局
    azoux's blog
    1004 成绩排名 PAT Basic Level
    1003 我要通过! PTA Basic Level
    腾讯云防盗链测试
    简单多项式求解
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/9472101.html
Copyright © 2011-2022 走看看