Java Reference
In-Depth Information
LISTING 11.5
Continued
17: ask[0] = new SurveyPanel(question1, responses1, 2);
18: String question2 = “What is your age?”;
19: String[] responses2 = { “Under 25”, “25-34”, “35-54”,
20: “Over 54” };
21: ask[1] = new SurveyPanel(question2, responses2, 1);
22: String question3 = “How often do you exercise each week?”;
23: String[] responses3 = { “Never”, “1-3 times”, “More than 3” };
24: ask[2] = new SurveyPanel(question3, responses3, 1);
25: ask[2].setFinalQuestion(true);
26: for (int i = 0; i < ask.length; i++) {
27: ask[i].nextButton.addActionListener(this);
28: ask[i].finalButton.addActionListener(this);
29: add(ask[i], “Card “ + i); .
30: }
31: }
32:
33: public void actionPerformed(ActionEvent evt) {
34: currentCard++;
35: if (currentCard >= ask.length) {
36: System.exit(0);
37: }
38: cards.show(this, “Card “ + currentCard);
39: }
40: }
41:
42: class SurveyPanel extends JPanel {
43: JLabel question;
44: JRadioButton[] response;
45: JButton nextButton = new JButton(“Next”);
46: JButton finalButton = new JButton(“Finish”);
47:
48: SurveyPanel(String ques, String[] resp, int def) {
49: super();
50: setSize(160, 110);
51: question = new JLabel(ques);
52: response = new JRadioButton[resp.length];
53: JPanel sub1 = new JPanel();
54: ButtonGroup group = new ButtonGroup();
55: JLabel quesLabel = new JLabel(ques);
56: sub1.add(quesLabel);
57: JPanel sub2 = new JPanel();
58: for (int i = 0; i < resp.length; i++) {
59: if (def == i) {
60: response[i] = new JRadioButton(resp[i], true);
61: } else {
62: response[i] = new JRadioButton(resp[i], false);
63: }
64: group.add(response[i]); .
65: sub2.add(response[i]);
11
Search WWH ::




Custom Search