fork download
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4.  
  5.  
  6.  
  7. public class PetSurvey extends JFrame
  8. {
  9. public static void main (String [] args)
  10. {
  11. new PetSurvey() ;
  12. }
  13.  
  14. JTextField name, phone, address;
  15. JRadioButton cat, dog , fish, oneOrMore , threeOrMore ;
  16. JCheckBox house, bungalow, appartment;
  17.  
  18. public PetSurvey()
  19. {
  20. this.setTitle ("Pet Survey");
  21. this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  22. JPanel panel = new JPanel ();
  23. panel.SetLayout ( new GridBagLayout());
  24.  
  25.  
  26.  
  27.  
  28. ButtonListener bl = new ButtonListener ();
  29.  
  30. addItem(panel, new JLabel ("Name"),
  31. 0, 0, 1, 1, GridBagConstraints.EAST);
  32. addItem(panel, new JLabel ("Phone: "),
  33. 0, 1, 1, 1, GridBagConstraints.EAST);
  34. name = new JTextField (20);
  35. phone = new JTextField(10);
  36.  
  37.  
  38. addItem(panel, name, 1, 0, 2, 1, GridBagConstraints.WEST);
  39. addItem(panel, phone, 1, 1, 1, 1, GridBagConstraints.WEST);
  40.  
  41.  
  42.  
  43.  
  44.  
  45. Box sizeBox = Box.createVerticalBox();
  46.  
  47. house = new JRadioButton("House");
  48. bungalow = new JRadioButton("bungalow");
  49. appartment = new JRadioButton("Appartment");
  50. ButtonGroup sizeGroup = new ButtonGroup();
  51. sizeGroup.add(house); //Adding buttons to button group
  52. sizeGroup.add(bungalow);
  53. sizeGroup.add(appartment);
  54.  
  55.  
  56. sizeBox.add(house);
  57. sizeBox.add(bungalow);
  58. sizeBox.add(appartment);
  59. sizeBox.setBorder(BorderFactory.createTitledBorder("Select your house"));
  60. addItem(panel, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH);
  61.  
  62.  
  63.  
  64. Box styleBox = Box.createVerticalBox ();
  65. oneOrMore = new JRadioButton("oneOrMore");
  66. threeOrMore = new JRadioButton("threeOrMore");
  67. ButtonGroup styleGroup = new ButtonGroup();
  68.  
  69. styleGroup.add(oneOrMore);
  70. styleGroup.add(threeOrMore);
  71. styleBox.add(oneOrMore);
  72. styleBox.add(threeOrMore);
  73. styleBox.setBorder(BorderFactory.createTitledBorder ("How many pets do you have"));
  74. addItem(panel, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH);
  75.  
  76. Box topBox = Box.createVerticalBox();
  77. house = new JCheckBox("House");
  78. bungalow = new JCheckBox("Bungalow");
  79. appartment = new JCheckBox("Appartment");
  80. topBox.add(house);
  81. topBox.add(bungalow);
  82. topBox.add(appartment);
  83. topBox.setBorder(BorderFactory.createTitledBorder("Where do you live"));
  84. addItem(panel, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH);
  85. Box buttonBox = Box.createHorizontalBox();
  86. okButton = new JButton("OK");
  87. okButton.addActionListener(bl);
  88.  
  89. closeButton = new JButton ("Close");
  90. closeButton.addActionListener(bl);
  91.  
  92. buttonBox.add(okButton);
  93. buttonBox.add(Box.createHorizontalStrut (20));
  94. buttonBox.add(closeButton);
  95. addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH);
  96. this.add(panel1);
  97. this.pack ();
  98. this.setVisible(true);
  99.  
  100. {
  101. private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
  102. }
  103.  
  104. gc.gridx = x;
  105. gc.gridy = y;
  106. gc.gridwidth = width;
  107. gc.gridheight = height;
  108. gc.weightx = 100.0;
  109. gc.weighty = 100.0;
  110. gc.insets = new Insets (5, 5, 5, 5);
  111. gc.anchor = align;
  112. gc.fill = GridBagConstraints.NONE;
  113. p.add(c, gc);
  114. }
  115.  
  116. private class ButtonListener implements ActionListener
  117.  
  118. {
  119. public void actionPerformed(ActionEvent e)
  120. {
  121. if (e.getSource() == okButton)
  122. {
  123.  
  124. String tops = "" ;
  125. if (house.isSelected()) tops +="House\n";
  126. if (bungalow.isSelected()) tops += "Bungalow \n";
  127. if (appartment.isSelected()) tops += "Appartment\n";
  128.  
  129. String base = " your base type is ";
  130. if (oneOrMore.isSelected()) base += "OneOrMore";
  131. if(threeOrMore.isSelected()) base += "ThreeOrMore";
  132.  
  133. String msg = "You ordered a ";
  134. if(small.isSelected()) msg += "...";
  135. if(medium.isSelected()) msg += "...";
  136. if (large.isSelected ()) msg += "...";
  137.  
  138.  
  139. //String name = name.getText;
  140.  
  141. if(tops.equals(""))
  142.  
  143. msg += "you have no pets";
  144. else
  145.  
  146. msg += "the following toppings:\n "+ tops + base;
  147.  
  148.  
  149. JOptionPane.showMessageDialog(okButton, msg, "Your Results!", JOptionPane.INFORMATION_MESSAGE);
  150.  
  151. house.setSelected(false);
  152. bungalow.setSelected(false);
  153. appartment.setSelected(false);
  154. cat.setSelected(false);
  155. dog.setSelected(true);
  156. fish.setSelected(true);
  157. }
  158.  
  159. if (e.getSource() == closeButton)
  160. {
  161. System.exit(0);
  162. }
  163. }
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:101: error: illegal start of expression
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
 ^
Main.java:101: error: illegal start of expression
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
         ^
Main.java:101: error: ';' expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                     ^
Main.java:101: error: ';' expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                          ^
Main.java:101: error: not a statement
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                           ^
Main.java:101: error: ';' expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                            ^
Main.java:101: error: <identifier> expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                    ^
Main.java:101: error: not a statement
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                         ^
Main.java:101: error: ';' expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                          ^
Main.java:101: error: <identifier> expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                                      ^
Main.java:101: error: not a statement
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                                           ^
Main.java:101: error: ';' expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                                                 ^
Main.java:101: error: ';' expected
 private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
                                                                                            ^
13 errors
stdout

Standard output is empty