RadioButton exam

Programming/JAVA,JSP 2013. 2. 3. 12:23 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 JRadioButton ane, cafela, cafemo;

    private JLabel text;

    private JPanel topPanel, sizePanel, resultPanel, itemPanel;

 

    private JPanel centerPanel;

   

    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);

 

        // centerPanel

        centerPanel = new JPanel();

        add(centerPanel, BorderLayout.CENTER);

        centerPanel.setLayout(new GridLayout(2,1));

       

        // sizePanel

        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);

        centerPanel.add(sizePanel);

 

        // itemPanel

        itemPanel = new JPanel();

        ane = new JRadioButton("아메리카노");

        cafela = new JRadioButton("카페라떼");

        cafemo = new JRadioButton("카페모카");

 

        ButtonGroup item = new ButtonGroup();

        item.add(ane);

        item.add(cafela);

        item.add(cafemo);

        ane.addActionListener(this);

        cafela.addActionListener(this);

        cafemo.addActionListener(this);

        itemPanel.add(ane);

        itemPanel.add(cafela);

        itemPanel.add(cafemo);

        centerPanel.add(itemPanel);

 

        // resultPanel

        resultPanel = new JPanel();

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

        text.setForeground(Color.red);

        resultPanel.add(text);

        add(resultPanel, BorderLayout.SOUTH);

        setVisible(true);

    }

 

    public void actionPerformed(ActionEvent e) {

        String itemString = null;

        String sizeString = null;

       

       

        if (ane.isSelected()) {

           itemString = "아메리카노";

        }

        else if (cafela.isSelected()) {

           itemString = "카페라떼";

        }

        else if (cafemo.isSelected()) {

           itemString = "카페모카";

        }

 

        if (small.isSelected()) {

           sizeString = "small";

        }

        else if (medium.isSelected()) {

           sizeString = "medium";

        }

        else if (large.isSelected()) {

           sizeString = "large";

        }

        text.setText(itemString + sizeString

               + " 주문하셨습니다.");

       

    }

}

 

public class RadioButtonTest extends JFrame {

    public static void main(String[] args) {

        new MyFrame();

    }

}

 

'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA JDBC 튜토리얼 예제 사이트  (0) 2013.02.24
JAVA 채팅 프로그램 + GUI  (0) 2013.02.23
[IT]자바의 위기, 한국 IT의 시금석  (0) 2013.02.02
JAVA 비행기게임  (0) 2013.01.09
JAVA 오목 게임 #1  (0) 2012.11.23