JAVA 프로그램을 만들면서 체크박스나 라디오 버튼들을 이용할 때가 있습니다.
먼저 체크박스를 이용해 그것을 누르면 내용을 출력할 수 있도록 만들었습니다.
취미보기 라는 버튼을 누르게 되면 체크한 것에 적은 내용이 나오게 해놨습니다.
코드입니다.
showHobby.java |
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Container;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
public class showHobby extends JFrame {
public Container contentPane;
public JPanel checkPane;
public JButton btnNext;
public JCheckBox[] ch;
String hobby[] = { "Exercise", "Read", "Music", "Cooking", "Draw", "Rise", "Game", "Dancing", "Travel", "Collect",
"Picture", "Movie", "Voluntary", "Computer" };
String[] hobbyi = new String[14];
int i = 0;
int cnt = 0;
JLabel lblProInput;
ArrayList<String> array = new ArrayList<String>();
public showHobby() {
setTitle("취미");
ch = new JCheckBox[14];
for (i = 0; i < 14; i++) {
ch[i] = new JCheckBox(hobby[i]);
ch[i].addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
cnt = e.getStateChange() == e.SELECTED ? (cnt += 1) : (cnt -= 1);
}
});
checkPane.add(ch[i]);
}
JButton btnNext = new JButton("취미보기");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (cnt >= 5) {
for (int i = 0; i < 14; i++) {
if (ch[i].isSelected()) {
array.add(hobby[i]);
}
}
String hobby1 = hobby[0], hobby2 = hobby[1], hobby3 = hobby[2], hobby4 = hobby[3],
hobby5 = hobby[4];
System.out.println("test" + hobby1);
CMain.muser.setHobby1(hobby1);
CMain.muser.setHobby2(hobby2);
CMain.muser.setHobby3(hobby3);
CMain.muser.setHobby4(hobby4);
CMain.muser.setHobby5(hobby5);
CMain.intr.show();
setVisible(false);
} else {
JOptionPane.showMessageDialog(contentPane, "5가지를 선택해주세요", "선택 오류", 0);
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(contentPane, "", "경고", 0);
}
}
});
}
}
댓글