Advertisement
CheeseKeg

WordCountWindow.java

Sep 29th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package com.cheesekeg.wordcount.gui;
  2.  
  3. import java.awt.FlowLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14.  
  15. import com.cheesekeg.wordcount.WordCount;
  16.  
  17. /**
  18.  * @author Brandon DeRosier
  19.  * @since 0.1
  20.  * @version 0.1
  21.  */
  22. public class WordCountWindow extends JFrame {
  23.  
  24.     private static final long serialVersionUID = 1L;
  25.    
  26.     private JLabel inputLabel, outputLabel;
  27.     private JTextArea inputArea, outputArea;
  28.     private JButton button;
  29.    
  30.     /**
  31.      * Construct a new WordCountWindow.
  32.      */
  33.     public WordCountWindow() {
  34.         populateGUI();
  35.        
  36.         //pack(); // Automatically size the window.
  37.        
  38.         addWindowListener(new WindowAdapter() {
  39.            
  40.             public void windowClosing(WindowEvent e) {
  41.                 System.exit(0);
  42.             }
  43.            
  44.         });
  45.        
  46.         setVisible(true); // Make the window visible.
  47.     }
  48.    
  49.     /**
  50.      * Initialize and apply elements to the window.
  51.      */
  52.     private void populateGUI() {
  53.         setResizable(false);
  54.         setSize(590, 570);
  55.        
  56.         setLayout(new FlowLayout());
  57.        
  58.         // Initialize the labels.
  59.         inputLabel = new JLabel("Input:", JLabel.CENTER);
  60.         outputLabel = new JLabel("Output:", JLabel.CENTER);
  61.        
  62.         // Initialize the TextAreas
  63.         inputArea = new JTextArea(15, 50);
  64.         outputArea = new JTextArea(15, 50);
  65.        
  66.         inputArea.setEditable(true);
  67.         outputArea.setEditable(false);
  68.        
  69.         // Initialize scroll panes to encapsulate the text areas.
  70.         JScrollPane inputScrollPane = new JScrollPane(inputArea,
  71.                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  72.                 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  73.         JScrollPane outputScrollPane = new JScrollPane(outputArea,
  74.                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  75.                 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  76.        
  77.         // Initialize the button.
  78.         button = new JButton("Sort by frequency");
  79.        
  80.         button.addActionListener(new ActionListener() {
  81.            
  82.             public void actionPerformed(ActionEvent e) {
  83.                 System.out.println("Sorting button pressed!");
  84.                 outputArea.setText(WordCount.CountWordString(inputArea.getText()));
  85.             }
  86.            
  87.         });
  88.        
  89.         // Add the elements to the window.
  90.         add(inputLabel);
  91.         add(inputScrollPane);
  92.         add(outputLabel);
  93.         add(outputScrollPane);
  94.         add(button);
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement