*텍스트와 레이블에 이미지 추가
1. ImageIcon 인스턴스를 생성하여야 한다.
> ImageIcon image = new ImageIcon;
2. setIcon() 메소드를 사용
> Jlabel label = new Jlabel("이미지 레이블");
> label.setIcon(image);
예제)
public class ImageLabelTest extends JFrame implements ActionListener{
private JPanel panel;
private JLabel label;
private JButton button;
public ImageLabelTest(){
this.setTitle("이미지 레이블");
this.setSize(300, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
label = new JLabel("이미지를 보려면 아래 버튼을 누르세요.");
button = new JButton("이미지 레이블");
ImageIcon icon = new ImageIcon("dog.png");
button.setIcon(icon);
button.addActionListener(this);
panel.add(label);
panel.add(button);
this.add(panel);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
ImageIcon dog = new ImageIcon("dog2.png");
label.setIcon(dog);
label.setText(null);
}
public static void main(String[] args) {
new ImageLabelTest();
}
'[JAVA]' 카테고리의 다른 글
[JAVA]10월 14일 스윙 컴포넌트, 리스트(List), 리스트의 이벤트 처리 예제 (0) | 2015.10.14 |
---|---|
[JAVA]10월 14일 스윙 컴포넌트, TextArea 를 활용한 예제.(KeyListner) (0) | 2015.10.14 |
[JAVA]10월 13일 GUI_Chat_Client, GUI_채팅 클라이언트 코드 예제 (0) | 2015.10.13 |
[JAVA]10월 13일 배치 관리자 FlowLayout, BorderLayout, GridLayout (0) | 2015.10.13 |
[JAVA]10월 13일 이벤트 처리 Action, Key, Mouse, MouseMotion (0) | 2015.10.13 |