doublemetal 2013. 7. 17. 01:06


Swing은 AWT 보다 약간 더 세련 되어졌다.

윈도 창의 종료 버튼의 이벤트를 추가하였다.

AWT와 마찬가지로 패널을 이용한다.



public class JButtonTest extends JFrame {

public JButtonTest() {

super("Test");

JPanel panel = new JPanel();

JButton btn = new JButton("Button");

panel.add(btn);


ImageIcon icon1 = new ImageIcon(".\\src\\1.png"); //기본 이미지

ImageIcon icon2 = new ImageIcon(".\\src\\2.png"); //눌렸을 때 이미지

JToggleButton tg = new JToggleButton(icon1); //토글버튼 생성

tg.setPressedIcon(icon2); //눌렀을 때의 이미지 세팅

panel.add(tg);


JLabel hobby = new JLabel("취미");

JCheckBox check1 = new JCheckBox("운동");

JCheckBox check2 = new JCheckBox("독서");

JCheckBox check3 = new JCheckBox("영화감상");

//레이블과 체크박스 객체들 생성

panel.add(hobby);

panel.add(check1);

panel.add(check2);

panel.add(check3); //패널에 붙임

JLabel male = new JLabel("성별");

JRadioButton rb1 = new JRadioButton("남자");

JRadioButton rb2 = new JRadioButton("여자");

ButtonGroup bg = new ButtonGroup();

//성별레이블 + 라이오버튼 2개 + 라디오그룹 객체 생성

bg.add(rb1);

bg.add(rb2); //라디오그룹에 라디오버튼을 붙이고 (같은 그룹)

panel.add(male);

panel.add(rb1);

panel.add(rb2); //패널에도 라디오버튼을 붙인다.

add(panel);

setLocation(150,200);

setSize(240,150);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

//종료 이벤트 설정

}


public static void main(String[] args) {

// TODO Auto-generated method stub

new JButtonTest();

}

}




취미는 토글버튼이며, png로 버튼이미지를 만들어보았다.