Crenox

MadLibs in Java

Apr 27th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. //Janty Kojakali
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. public class MadLibs extends JApplet implements ActionListener
  6. {
  7.  
  8. JTextArea area1;
  9.  
  10. JPanel panel1;
  11. JPanel panel2;
  12. JPanel panel3;
  13.  
  14. JButton button1;
  15.  
  16. JRadioButton radio1;
  17. JRadioButton radio2;
  18. JRadioButton radio3;
  19. JRadioButton radio4;
  20. JRadioButton radio5;
  21. JRadioButton radio6;
  22.  
  23. JTextField field1;
  24. JTextField field2;
  25. JTextField field3;
  26. JTextField field4;
  27. JTextField field5;
  28. JTextField field6;
  29. JTextField field7;
  30. JTextField field8;
  31. JTextField field9;
  32. JTextField field10;
  33.  
  34. ButtonGroup group1;
  35. ButtonGroup group2;
  36. ButtonGroup group3;
  37.  
  38.  
  39.  
  40. Font font;
  41.  
  42. String a,b,c,d,f,g,h,i,j,x,k;
  43. int l,m;
  44. public void init()
  45. {
  46. Container cp;
  47. cp = getContentPane();
  48. cp.setLayout(new GridLayout(2,1));
  49.  
  50. field1 = new JTextField("Enter a number");
  51. field2 = new JTextField("Enter an adjective");
  52. field3 = new JTextField("Enter an adjective");
  53. field4 = new JTextField("Enter a noun");
  54. field5 = new JTextField("Enter an adverb");
  55. field6 = new JTextField("Enter a noun");
  56. field7 = new JTextField("Enter a plural noun");
  57. field8 = new JTextField("Enter an adjective");
  58. field9 = new JTextField("Enter a noun");
  59. field10 = new JTextField("Enter a noun");
  60.  
  61. radio1 = new JRadioButton("Arial");
  62. radio2 = new JRadioButton("TimesNewRoman");
  63. radio3 = new JRadioButton("12");
  64. radio4 = new JRadioButton("14");
  65. radio5 = new JRadioButton("Bold");
  66. radio6 = new JRadioButton("Italic");
  67.  
  68. ButtonGroup group1 = new ButtonGroup();
  69. ButtonGroup group2 = new ButtonGroup();
  70. ButtonGroup group3 = new ButtonGroup();
  71.  
  72. button1 = new JButton("Submit");
  73.  
  74. area1 = new JTextArea(20,100);
  75.  
  76. panel1 = new JPanel();
  77. panel2 = new JPanel();
  78. panel3 = new JPanel();
  79.  
  80. cp.add(panel1);
  81. cp.add(panel2);
  82. cp.add(panel3);
  83.  
  84. panel1.add(button1);
  85.  
  86. group1.add(radio1);
  87. group1.add(radio2);
  88. group2.add(radio3);
  89. group2.add(radio4);
  90. group3.add(radio5);
  91. group3.add(radio6);
  92.  
  93. panel1.add(field1);
  94. panel1.add(field2);
  95. panel1.add(field3);
  96. panel1.add(field4);
  97. panel1.add(field5);
  98. panel1.add(field6);
  99. panel1.add(field7);
  100. panel1.add(field8);
  101. panel1.add(field9);
  102. panel1.add(field10);
  103.  
  104. panel2.add(radio1);
  105. panel2.add(radio2);
  106. panel2.add(radio3);
  107. panel2.add(radio4);
  108. panel2.add(radio5);
  109. panel2.add(radio6);
  110.  
  111. panel3.add(area1);
  112.  
  113. setLayout(new FlowLayout());
  114.  
  115. panel1.setLayout(new FlowLayout());
  116. panel2.setLayout(new FlowLayout());
  117. panel3.setLayout(new FlowLayout());
  118.  
  119.  
  120. button1.addActionListener(this);
  121.  
  122. field1.addActionListener(this);
  123. field2.addActionListener(this);
  124. field3.addActionListener(this);
  125. field4.addActionListener(this);
  126. field5.addActionListener(this);
  127. field6.addActionListener(this);
  128. field7.addActionListener(this);
  129. field8.addActionListener(this);
  130. field9.addActionListener(this);
  131. field10.addActionListener(this);
  132.  
  133. radio1.addActionListener(this);
  134. radio2.addActionListener(this);
  135. radio3.addActionListener(this);
  136. radio4.addActionListener(this);
  137. radio5.addActionListener(this);
  138. radio6.addActionListener(this);
  139.  
  140. }
  141.  
  142.  
  143. public void actionPerformed (ActionEvent e)
  144. {
  145.  
  146.  
  147. if (e.getSource() == button1)
  148. {
  149. a = field1.getText();
  150. b = field2.getText();
  151. c = field3.getText();
  152. d = field4.getText();
  153. x = field5.getText();
  154. f = field6.getText();
  155. g = field7.getText();
  156. h = field8.getText();
  157. i = field9.getText();
  158. j = field10.getText();
  159.  
  160. if(radio1.isSelected())
  161. {
  162. k = "Arial";
  163. }
  164.  
  165. if(radio2.isSelected())
  166. {
  167. k = "Times New Roman";
  168. }
  169.  
  170. if(radio3.isSelected())
  171. {
  172. m = 12;
  173. }
  174.  
  175. if(radio4.isSelected())
  176. {
  177. m = 14;
  178. }
  179. if(radio5.isSelected())
  180. {
  181. l = Font.BOLD;
  182. }
  183. if(radio6.isSelected())
  184. {
  185. l = Font.ITALIC;
  186. }
  187.  
  188. Font font = new Font(k, l, m);
  189. area1.setFont(font);
  190. area1.setText("There was a boy who was " + a + " years old. He wanted to prove himself so he set out on a journey. During his journey he ran into a " + b + "," + c + " " + d + ". Prepared for a situation like this, he " + x + " pull out his " + f + ". After defeating the " + d + " , he gets hungry so he eats " + g + " with some ketchup. After eating he got thirsty so he drank some " + h + "," + i + " with his " + j + ".He then dies of food poisoning. THE END!!" );
  191. area1.setLineWrap(true);
  192. }
  193.  
  194.  
  195.  
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment