티스토리 뷰
# hyperlink in JLabel
AWT, Swing 과 같은 Java GUI 를 이용한 개발시에
텍스트 라벨등을 클릭한 경우 HTTP 접속, Email 연결이 되도록 하는 방법.
/**
* Example of a jLabel Hyperlink and a jLabel Mailto
*/
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author ibrabelware
*/
public class JLabelLink extends JFrame {
private JPanel pan;
private JLabel contact;
private JLabel website;
/**
* Creates new form JLabelLink
*/
public JLabelLink() {
this.setTitle("jLabelLinkExample");
this.setSize(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
pan = new JPanel();
contact = new JLabel();
website = new JLabel();
contact.setText("<html> contact : <a href=\"\">YourEmailAddress@gmail.com</a></html>");
contact.setCursor(new Cursor(Cursor.HAND_CURSOR));
website.setText("<html> Website : <a href=\"\">http://www.google.com/</a></html>");
website.setCursor(new Cursor(Cursor.HAND_CURSOR));
pan.add(contact);
pan.add(website);
this.setContentPane(pan);
this.setVisible(true);
sendMail(contact);
goWebsite(website);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Create and display the form
*/
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JLabelLink().setVisible(true);
}
});
}
private void goWebsite(JLabel website) {
website.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(new URI("http://www.google.com/webhp?nomo=1&hl=fr"));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
});
}
private void sendMail(JLabel contact) {
contact.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().mail(new URI("mailto:YourEmailAddress@gmail.com?subject=TEST"));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
});
}
}
'Computer > JAVA' 카테고리의 다른 글
Java Error List (0) | 2018.04.16 |
---|---|
[Java] JDK 설치 및 환경변수 설정 (0) | 2017.12.11 |
JavaService 등록/해제 (윈도우부팅시 자바프로그램 자동실행) (0) | 2017.03.11 |
[Eclipse] WindowBuilder Java GUI(AWT,SWT,Swing) Tool (0) | 2017.03.11 |
JavaService 자바프로그램 부팅시 자동실행 (0) | 2017.02.28 |
댓글