삼성SDS_멀티캠퍼스/Java

25일 차 Checkbox(체크박스)

박성우기 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();

}

}




반응형