1. package gui;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import java.util.Arrays; // Zelf toegevoegd
  7. import java.util.Collection;
  8. import java.util.Collections; // Zelf toegevoegd
  9. import java.util.Comparator; // Zelf toegevoegd
  10. import javax.swing.BorderFactory;
  11. import java.util.Iterator; // Zelf toegevoegd
  12. import javax.swing.JButton;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16. import java.util.List; // Zelf toegevoegd
  17. import javax.swing.WindowConstants;
  18. import javax.swing.SwingUtilities;
  19.  
  20.  
  21. /**
  22. * This code was edited or generated using CloudGarden's Jigloo
  23. * SWT/Swing GUI Builder, which is free for non-commercial
  24. * use. If Jigloo is being used commercially (ie, by a corporation,
  25. * company or business for any purpose whatever) then you
  26. * should purchase a license for each developer using Jigloo.
  27. * Please visit www.cloudgarden.com for details.
  28. * Use of Jigloo implies acceptance of these licensing terms.
  29. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
  30. * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
  31. * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
  32. */
  33. public class WoordenFrame extends javax.swing.JFrame {
  34.  
  35. private static final long serialVersionUID = 1L;
  36. private JPanel pnInput;
  37. private JPanel pnOutput;
  38. private JButton btConcordantie;
  39. private JButton btFrequentie;
  40. private JButton btSorteer;
  41. private JButton btAantal;
  42. private JScrollPane spInput;
  43. private JTextArea taOutput;
  44. private JTextArea taInput;
  45. private JScrollPane spOutput;
  46.  
  47.  
  48.  
  49. /**
  50. * Auto-generated main method to display this JFrame
  51. */
  52. public static void main(String[] args) {
  53. SwingUtilities.invokeLater(new Runnable() {
  54. public void run() {
  55. WoordenFrame inst = new WoordenFrame();
  56. inst.setLocationRelativeTo(null);
  57. inst.setVisible(true);
  58. }
  59. });
  60. }
  61.  
  62. public WoordenFrame() {
  63. super();
  64. initGUI();
  65. }
  66.  
  67. private void initGUI() {
  68. try {
  69. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  70. this.setTitle("Woorden");
  71. getContentPane().setLayout(null);
  72. {
  73. pnInput = new JPanel();
  74. getContentPane().add(pnInput);
  75. pnInput.setLayout(null);
  76. pnInput.setBounds(12, 12, 368, 124);
  77. pnInput.setBorder(BorderFactory.createTitledBorder("Input"));
  78. pnInput.setLayout(null);
  79. {
  80. btAantal = new JButton();
  81. pnInput.add(btAantal);
  82. btAantal.setText("Aantal");
  83. btAantal.setBounds(243, 20, 114, 21);
  84. btAantal.addActionListener(new ActionListener() {
  85. public void actionPerformed(ActionEvent evt) {
  86. btAantalActionPerformed(evt);
  87. }
  88. });
  89. }
  90. {
  91. btSorteer = new JButton();
  92. pnInput.add(btSorteer);
  93. btSorteer.setText("Sorteer");
  94. btSorteer.setBounds(243, 44, 114, 21);
  95. btSorteer.addActionListener(new ActionListener() {
  96. public void actionPerformed(ActionEvent evt) {
  97. btSorteerActionPerformed(evt);
  98. }
  99. });
  100. }
  101. {
  102. btFrequentie = new JButton();
  103. pnInput.add(btFrequentie);
  104. btFrequentie.setText("Frequentie");
  105. btFrequentie.setBounds(243, 68, 114, 21);
  106. btFrequentie.addActionListener(new ActionListener() {
  107. public void actionPerformed(ActionEvent evt) {
  108. btFrequentieActionPerformed(evt);
  109. }
  110. });
  111. }
  112. {
  113. btConcordantie = new JButton();
  114. pnInput.add(btConcordantie);
  115. btConcordantie.setText("Concordantie");
  116. btConcordantie.setBounds(243, 93, 114, 21);
  117. btConcordantie.addActionListener(new ActionListener() {
  118. public void actionPerformed(ActionEvent evt) {
  119. btConcordantieActionPerformed(evt);
  120. }
  121. });
  122. }
  123. {
  124. spInput = new JScrollPane();
  125. pnInput.add(spInput);
  126. spInput.setBounds(12, 19, 219, 95);
  127. {
  128. taInput = new JTextArea();
  129. spInput.setViewportView(taInput);
  130. }
  131. }
  132. }
  133. {
  134. pnOutput = new JPanel();
  135. getContentPane().add(pnOutput);
  136. pnOutput.setBorder(BorderFactory.createTitledBorder("Output"));
  137. pnOutput.setBounds(11, 142, 367, 121);
  138. pnOutput.setLayout(null);
  139. {
  140. spOutput = new JScrollPane();
  141. pnOutput.add(spOutput);
  142. spOutput.setBounds(12, 19, 344, 89);
  143. {
  144. taOutput = new JTextArea();
  145. spOutput.setViewportView(taOutput);
  146. }
  147. }
  148. }
  149. pack();
  150. setSize(400, 300);
  151. //Standaard tekst, effe voor de handigheid.
  152. taInput.setText("een, twee, drie, vier,\nhoedje van, hoedje van,\neen, twee, drie, vier,\nhoedje van papier.");
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. private void btAantalActionPerformed(ActionEvent evt) {
  159. //Reset de output even
  160. taOutput.setText("");
  161.  
  162. //Pak de input string en split het op een aantal karakters
  163. String delims = "[ .,?!\n]+";
  164. String[] alleWoordenArray = taInput.getText().split(delims);
  165. List alleWoorden = Arrays.asList(alleWoordenArray);
  166.  
  167. //Laat zien dat het werkt
  168. taOutput.setText(taOutput.getText() + "Totaal aantal woorden: "+Integer.toString(alleWoorden.size()) +"\n");
  169.  
  170. //Voeg elk woord toe wat nog niet in de unieke woorden array zit
  171. List uniekeWoorden = new ArrayList();
  172. Iterator iterator = alleWoorden.iterator();
  173. while (iterator.hasNext())
  174. {
  175. String woord = (String) iterator.next();
  176. if (!uniekeWoorden.contains(woord))
  177. {
  178. uniekeWoorden.add(woord);
  179. //taOutput.setText(taOutput.getText() + woord +"\n");
  180. }
  181. }
  182.  
  183. //Laat zien dat dit ook goed gelukt is
  184. taOutput.setText(taOutput.getText() + "Aantal verschillende woorden: "+Integer.toString(uniekeWoorden.size()) +"\n");
  185. }
  186.  
  187. private void btSorteerActionPerformed(ActionEvent evt) {
  188. //Reset de output even
  189. taOutput.setText("");
  190.  
  191. //Pak de input string en split het op een aantal karakters
  192. String delims = "[ .,?!\n]+";
  193. String[] alleWoordenArray = taInput.getText().split(delims);
  194. List alleWoorden = Arrays.asList(alleWoordenArray);
  195.  
  196. //Voeg elk woord toe wat nog niet in de unieke woorden array zit
  197. List uniekeWoorden = new ArrayList();
  198. Iterator iterator = alleWoorden.iterator();
  199. while (iterator.hasNext())
  200. {
  201. String woord = (String) iterator.next();
  202. if (!uniekeWoorden.contains(woord))
  203. {
  204. uniekeWoorden.add(woord);
  205. }
  206. }
  207.  
  208. //Maak een comparator voor analfabetische volgorde
  209. class analfabetischComp implements Comparator<String>
  210. {
  211. // Gebruik de gewone string comparator, maar wissel de argumenten om
  212. public int compare(String strA, String strB)
  213. {
  214. // NOTENOTE: Let op waar strB en strA staat!
  215. return strB.compareTo(strA);
  216. }
  217. }
  218.  
  219. //Sorteer de lijst allfabetisch
  220. Collections.sort(uniekeWoorden, new analfabetischComp());
  221.  
  222. //Laat zien dat dit ook goed gelukt is
  223. iterator = uniekeWoorden.iterator();
  224. while (iterator.hasNext())
  225. {
  226. String woord = (String) iterator.next();
  227. taOutput.setText(taOutput.getText() + woord +"\n");
  228. }
  229. }
  230.  
  231. private void btFrequentieActionPerformed(ActionEvent evt) {
  232. // to do
  233. }
  234.  
  235. private void btConcordantieActionPerformed(ActionEvent evt) {
  236. // to do
  237. }
  238.  
  239. }
  240.