처음 이 문제를 봤을 때는 그런 생각이 안 들었는데,
하다 보니까 이걸 만약 for문을 이용해서 푼다면 어떻게 될까? 하면서 한 번…
package wed160525; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Created on 2016-05-29. */ public class pizza extends JFrame implements ActionListener{ private JLabel label = new JLabel("자바 피자에 오신 것을 환영합니다."); private JButton button1 = new JButton("확인"); private JButton button2 = new JButton("취소"); private JPanel panel = new JPanel(); private JPanel p[] = new JPanel[5]; private JPanel southPanel1 = new JPanel(); private JPanel southPanel2 = new JPanel(); private int count = 0; private int count2 = 0; private String str[] = { "콤보","포테이토","불고기", "Small","Medium","Large", }; private String str2[] = { "종류", "추가 토핑","크기" }; private String str3[] = { "피망", "치즈", "페페로니", "베이컨" }; private JTextField tf = new JTextField(10); private JRadioButton btn[] = new JRadioButton[6]; private JCheckBox boxes[] = new JCheckBox[4]; private ButtonGroup group1 = new ButtonGroup(); private ButtonGroup group2 = new ButtonGroup(); private pizza() { this.setTitle("피자 주문"); this.setSize(450,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout(new BorderLayout()); for(int i = 0; i <= 5; i++) { btn[i] = new JRadioButton(str[i]); if(i <= 2) { group1.add(btn[i]); } else { group2.add(btn[i]); } } for(int i = 0; i <= 4 ; i++) { p[i] = new JPanel(); } for(int i = 0; i <= 3 ; i++) { boxes[i] = new JCheckBox(str3[i]); p[2].add(boxes[i]); } label.setForeground(Color.red); p[0].add(label); p[1].setLayout(new GridLayout(3,1)); p[2].setLayout(new GridLayout(4,1)); p[3].setLayout(new GridLayout(3,1)); for(int i = 1; i <= 3; i+=2) { for(int j = 0; j < 3; j++) { p[i].add(btn[count]); count++; } } for(int i = 1; i <= 3; i++) { Border border = BorderFactory.createTitledBorder(str2[i-1]); p[i].setBorder(border); } p[4].setLayout(new GridLayout(2,1)); tf.setEditable(false); button1.addActionListener(this); button2.addActionListener(this); southPanel2.add(button1); southPanel2.add(button2); p[4].add(tf); p[4].add(southPanel2); panel.add(p[0], BorderLayout.NORTH); panel.add(p[1], BorderLayout.WEST); panel.add(p[2], BorderLayout.CENTER); panel.add(p[3], BorderLayout.EAST); panel.add(p[4], BorderLayout.SOUTH); this.add(panel); this.setVisible(true); } public static void main(String[] args) { new pizza(); } @Override public void actionPerformed(ActionEvent e) { String radio1 = ""; String radio2 = ""; String boxesChecked = ""; if(e.getSource() == button1) { for(int i = 0; i <= 5; i++) { if(i <= 3) { if(boxes[i].isSelected()) { boxesChecked += boxes[i].getText() + " "; } } if(btn[i].isSelected() && i <= 2) { radio1 = btn[i].getText(); } else if(btn[i].isSelected()) { radio2 = btn[i].getText(); } } tf.setText(radio1 + " " + boxesChecked + " " + radio2 + ": 피자 준비 완료"); } else if(e.getSource() == button2) { tf.setText(""); for(int i = 0; i <= 3; i++) boxes[i].setSelected(false); group1.clearSelection(); group2.clearSelection(); } } }