JRadioButtonTest

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

 

import javax.swing.*;

import javax.swing.border.Border;

 

import java.awt.event.*;

import java.awt.*;

 

class MyFrame extends JFrame implements ActionListener {

 

       private JRadioButton small, medium, large;

       private JLabel text;

       private JPanel topPanel, sizePanel, resultPanel;

 

       public MyFrame() {

             setTitle("라디오 버튼 테스트");

             setSize(300, 150);

             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

             topPanel = new JPanel();

             JLabel label = new JLabel("어떤 크기의 커피를 주문하시겠습니까?");

             topPanel.add(label);

             add(topPanel, BorderLayout.NORTH);

 

          sizePanel = new JPanel();

             small = new JRadioButton("Small Size");

             medium = new JRadioButton("Medium Size");

             large = new JRadioButton("Large Size");

 

             ButtonGroup size = new ButtonGroup();

             size.add(small);

             size.add(medium);

             size.add(large);

             small.addActionListener(this);

             medium.addActionListener(this);

             large.addActionListener(this);

             sizePanel.add(small);

             sizePanel.add(medium);

             sizePanel.add(large);

             add(sizePanel, BorderLayout.CENTER);

 

             resultPanel = new JPanel();

             text = new JLabel("크기가 선택되지 않았습니다.");

             text.setForeground(Color.red);

             resultPanel.add(text);

             add(resultPanel, BorderLayout.SOUTH);

             setVisible(true);

       }

public void actionPerformed(ActionEvent e) {

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

                    text.setText("Small 크기가 선택되었습니다.");

             }

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

                    text.setText("Medium 크기가 선택되었습니다.");

             }

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

                    text.setText("Large 크기가 선택되었습니다.");

             }

       }

}

 

public class RadioButtonTest extends JFrame {

       public static void main(String[] args) {

             new MyFrame();

       }

 }

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

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