Advertisement
t805

String Check GUI

Sep 16th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package week9;
  2.  
  3. import javax.swing.*;  //for JFrame, JTextField, JTextArea
  4. import java.awt.*;    //for Container, BorderLayout
  5. import java.awt.event.*; //for WindowAdapter, ActionListener, ActionEvent
  6.  
  7. public class TextGUI extends JFrame {
  8.    
  9.     private static final int WIDTH = 800;      
  10.     private static final int HEIGHT = 500; 
  11.  
  12.     private JLabel infoLabel;
  13.     private JTextField textField;
  14.     private JTextArea textArea;
  15.     private final static String newline = "\n";
  16.    
  17.    
  18.     private class EnterAction implements ActionListener
  19.     {
  20.             public void actionPerformed(ActionEvent e)
  21.             {
  22.                 //StringCompare c = new StringCompare();
  23.                
  24.                 String text = textField.getText();
  25.                
  26.                 if(StringCompare.Compare(text) == true)
  27.                     textArea.append(text + newline + "There are repeated characters." + newline);
  28.                 else
  29.                     textArea.append(text + newline + "There are no repeated characters." + newline);
  30.                    
  31.                 textField.selectAll();
  32.                 textArea.setCaretPosition(textArea.getDocument().getLength());
  33.             }
  34.     }
  35.    
  36.     private class WindowDestroyer extends WindowAdapter
  37.     {
  38.         public void windowClosing(WindowEvent e)
  39.         {
  40.             dispose();
  41.             System.exit(0);
  42.         }
  43.     }
  44.    
  45.     public TextGUI(String windowTitle)
  46.     {
  47.         super(windowTitle);
  48.     setSize(WIDTH, HEIGHT);
  49.      
  50.         Container c1 = getContentPane();
  51.         c1.setLayout( new BorderLayout());
  52.  
  53.     textField = new JTextField();
  54.     c1.add(textField, BorderLayout.NORTH);
  55.        
  56.         textArea = new JTextArea();
  57.     c1.add( textArea, BorderLayout.CENTER);
  58.         textArea.setEditable(false);
  59.        
  60.        
  61.     EnterAction myAction = new EnterAction();
  62.     textField.addActionListener(myAction);
  63.  
  64.     WindowDestroyer myListener = new WindowDestroyer();
  65.     addWindowListener(myListener);
  66.  
  67.     }
  68.    
  69.     public static void main(String[] args)
  70.     {
  71.     TextGUI app = new TextGUI("Zzzz");
  72.    
  73.     app.setVisible(true);
  74.  
  75.         System.out.println("Finished SimplApp.main()");
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement