SORU
9 ŞUBAT 2009, PAZARTESİ


Nasıl JLabel köprü eklemek için

Hangi jLabel bir köprü eklemek için en iyi yolu nedir? Görünümü html etiketlerini kullanarak alabilirsiniz, ama tarayıcı açmak için nasıl kullanıcı tıklattığında?

CEVAP
9 ŞUBAT 2009, PAZARTESİ


JButton Bu stil için JLabel, ama bir alternatif olacak bir kullanarak yapabilirsiniz. Bu şekilde, accessibility hakkında endişelenmenize gerek yok ve sadece olayları ActionListener kullanarak ateş edebilirsiniz.

  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 */ }
  }

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • A Alkhaldi

    A Alkhaldi

    12 Mayıs 2007
  • george sarintzotis

    george sarin

    2 Aralık 2007
  • MrDevin521

    MrDevin521

    18 Temmuz 2010