怎么在jlabel 上点下就到指定的一个网址 ??

解决方案 »

  1.   


    import java.awt.Cursor;
    import java.awt.EventQueue;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;import javax.swing.JFrame;
    import javax.swing.JLabel;public class Test extends JFrame {  /**
       * Launch the application
       * 
       * @param args
       */
      public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() {
            try {
              Test frame = new Test();
              frame.setVisible(true);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
      }  /**
       * Create the frame
       */
      public Test() {
        super();
        getContentPane().setLayout(null);
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    final JLabel label = new JLabel();
        label.addMouseListener(new MouseAdapter() {
          public void mouseClicked(final MouseEvent e) {
            String cmd = "rundll32 url.dll, FileProtocolHandler "
                + label.getText().trim();
            try {
              Process p = Runtime.getRuntime().exec(cmd);
            } catch (IOException e1) {
              e1.printStackTrace();
            }      }
        });
        label.setText("http://www.baidu.com");
        label.setCursor(new Cursor(Cursor.HAND_CURSOR));
        label.setBounds(80, 102, 281, 32);
        getContentPane().add(label);
        //
      }}