JCheckBoxTest

실습과제 모음 2012. 7. 18. 17:52 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

 

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.applet.Applet;

 

import javax.swing.*;

 

class MyFrame extends JFrame implements ActionListener {

 

       private JButton buttonOK;

       private JCheckBox onion, cheese, tomato;

 

       public MyFrame() {

             setTitle("체크박스 테스트");

             setSize(300, 130);

             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

             JLabel label = new JLabel("햄버거에 무엇을 추가하시겠습니까?");

             JPanel topPanel = new JPanel();

             topPanel.add(label);

             add(topPanel, BorderLayout.NORTH);

  // 체크 박스 생성 추가

             JPanel panel = new JPanel();

             onion = new JCheckBox("양파");

             panel.add(onion);

             cheese = new JCheckBox("치즈");

             panel.add(cheese);

             tomato = new JCheckBox("토마토");

             panel.add(tomato);

             add(panel, BorderLayout.CENTER);

 

             // 버튼 생성 추가

             buttonOK = new JButton("OK");

             JPanel bottomPanel = new JPanel();

             bottomPanel.add(buttonOK);

             add(bottomPanel, BorderLayout.SOUTH);

             buttonOK.addActionListener(this);

 

             setVisible(true);

       }

 

public void actionPerformed(ActionEvent e) {

             if (e.getSource() == buttonOK) {

                    String msg = "";

                    if (onion.isSelected())

                           msg += "양파\n";

                    if (cheese.isSelected())

                           msg += " 치즈";

                    if (tomato.isSelected())

                           msg += " 토마토";

                    msg = "선택한 옵션은 다음과 같습니다.\n " + msg;

                    System.out.println(msg);

                    onion.setSelected(false);

                    cheese.setSelected(false);

                    tomato.setSelected(false);

             }

       }

}

 

public class JCheckBoxTest extends JFrame {

       public static void main(String[] args) {

             new MyFrame();

       }

}

'실습과제 모음' 카테고리의 다른 글

방학특강 C 언어 주말과제 0720~0723  (0) 2012.07.20
JRadioButtonTest  (0) 2012.07.18
java 실습 0717  (0) 2012.07.17
JAVA ComboBoxTest  (0) 2012.07.14
CPP 실습문제 0714  (0) 2012.07.14