-
25일 차 Checkbox(체크박스)삼성SDS_멀티캠퍼스/Java 2015. 10. 14. 09:40반응형
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Checkbox extends JFrame implements ActionListener {
private JButton buttonOK;
private JCheckBox onion, cheese, tomato;
public Checkbox() {
this.setTitle("체크박스");
this.setSize(300, 130);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("햄버거에 뭘 추가하겠습니까");
JPanel topPanel = new JPanel();
topPanel.add(label);
this.add(topPanel, BorderLayout.NORTH);
JPanel panel = new JPanel();
onion = new JCheckBox("양파");
cheese = new JCheckBox("치즈");
tomato = new JCheckBox("토마토");
panel.add(onion);
panel.add(cheese);
panel.add(tomato);
this.add(panel, BorderLayout.CENTER);
buttonOK = new JButton("OK");
JPanel bottomPanel = new JPanel();
bottomPanel.add(buttonOK);
this.add(bottomPanel, BorderLayout.SOUTH);
buttonOK.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == buttonOK) {
String msg = "\n";
if (onion.isSelected())
msg += "양파\n";
if (cheese.isSelected())
msg += "치즈\n";
if (tomato.isSelected())
msg += "토마토\n";
msg = "선택한 옵션은 다음과 같습니다.\n" + msg;
System.out.println(msg);
onion.setSelected(false);
cheese.setSelected(false);
tomato.setSelected(false);
}
}
public static void main(String[] args) {
new Checkbox();
}
}
반응형'삼성SDS_멀티캠퍼스 > Java' 카테고리의 다른 글
25일 차 Radiobutton(라디오버튼) (5) 2015.10.14 24일 차 Flow, Border, Grid Layout (0) 2015.10.13 24일 차 마우스이벤트 편리하게 사용하는법 (0) 2015.10.13 24일 차 마우스이벤트 (0) 2015.10.13 24일 차 KeyEvent (0) 2015.10.13