Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. import java.awt.GridBagConstraints;
  2. import java.awt.GridBagLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.BufferedInputStream;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.PrintWriter;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Calendar;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextArea;
  20. import javax.swing.JTextField;
  21. import javax.swing.UIManager;
  22. import javax.swing.UnsupportedLookAndFeelException;
  23. public class display extends JPanel implements ActionListener
  24. {
  25.     protected static JTextField textField;
  26.     protected JTextArea textArea;
  27.     private final static String newline = "\n";
  28.     String dir = System.getProperty("user.dir");
  29.     static File file = new File("chat.html");
  30.     static boolean exists = (file).exists();
  31.     public static final String DATE_FORMAT_NOW = "HH:mm:ss";   
  32.     public static String now()
  33.     {
  34.         Calendar cal = Calendar.getInstance();
  35.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  36.         return sdf.format(cal.getTime());      
  37.     }
  38.          
  39.     private static void createAndShowGUI() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
  40.     {
  41.         //Create and set up the window.
  42.         JFrame.setDefaultLookAndFeelDecorated(true);
  43.         UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());        
  44.         JFrame frame = new JFrame("Chat");
  45.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.        
  47.         //Add contents to the window.
  48.         frame.add(new TestHelp());
  49.        
  50.         //TestHelp the window.
  51.         frame.pack();
  52.         frame.setVisible(true);
  53.     }
  54.     public void actionPerformed(ActionEvent event)
  55.     {
  56.         //the code it executes when the button is pressed
  57.         boolean exists = (file).exists();
  58.         byte[] c = new byte[1024];
  59.         int count = 0;
  60.         int readChars = 0;
  61.         if(exists)
  62.         {
  63.             try
  64.             {
  65.                 String input = textField.getText();
  66.                 if(input.length() != 0)
  67.                 {
  68.                     InputStream is = new BufferedInputStream(new FileInputStream(file));
  69.                     FileInputStream in = new FileInputStream("chat.html");
  70.                     BufferedReader br = new BufferedReader(new InputStreamReader(in));
  71.                     while ((readChars = is.read(c)) != -1)
  72.                     {
  73.                         for (int i = 0; i < readChars; ++i)
  74.                         {
  75.                             if (c[i] == '\n')
  76.                                 ++count;
  77.                            
  78.                         }
  79.                        
  80.                     }
  81.                     String[] read;
  82.                     read = new String[count];
  83.                     for (int counter = 0; counter < read.length; counter++)
  84.                     {
  85.                         read[counter] = br.readLine(); 
  86.                     }
  87.                     in.close();
  88.                     PrintWriter prnt = new PrintWriter(file);
  89.                     for(int index=0;index<read.length; index++)
  90.                     {
  91.                         //Print the old file contents
  92.                         prnt.println(read[index]); 
  93.                     }
  94.                     prnt.println("<" +now() +">" +input);
  95.                     prnt.close();  
  96.                 }  
  97.             }
  98.             catch(NullPointerException e)
  99.             {
  100.                 e.printStackTrace();   
  101.             }
  102.             catch(FileNotFoundException e)
  103.             {
  104.                 e.printStackTrace();   
  105.             }
  106.             catch(IOException e)
  107.             {
  108.                 e.printStackTrace();   
  109.             }  
  110.         }
  111.         else//if it does not exist
  112.         {
  113.            
  114.         }  
  115.     }
  116.     public static void main(String[] args) throws FileNotFoundException{
  117.         //if(!exists){//if chat.html doesn't exists,
  118.         //PrintWriter prnt = new PrintWriter(file);
  119.         //prnt.println("");
  120.         //prnt.close();
  121.         //}
  122.         //Schedule a job for the event dispatch thread:
  123.         //creating and showing this application's GUI.
  124.         javax.swing.SwingUtilities.invokeLater(new Runnable()
  125.         {
  126.             public void run()
  127.             {
  128.                 try
  129.                 {
  130.                     createAndShowGUI();
  131.                 }
  132.                 catch (ClassNotFoundException e)
  133.                 {
  134.                     e.printStackTrace();   
  135.                 }
  136.                 catch (InstantiationException e)
  137.                 {
  138.                     e.printStackTrace();   
  139.                 }
  140.                 catch (IllegalAccessException e)
  141.                 {
  142.                     e.printStackTrace();           
  143.                 }
  144.                 catch (UnsupportedLookAndFeelException e)
  145.                 {
  146.                     e.printStackTrace();   
  147.                 }  
  148.             }  
  149.         });
  150.         //else{}//if it exists, no use yet.
  151.     }
  152.     public display()
  153.     {
  154.         super(new GridBagLayout());
  155.        
  156.         textField = new JTextField(20);
  157.         textField.addActionListener(this);
  158.  
  159.         textArea = new JTextArea(5, 20);
  160.         textArea.setEditable(false);    
  161.         JScrollPane scrollPane = new JScrollPane(textArea);
  162.  
  163.         //Add Components to this panel.
  164.         GridBagConstraints c = new GridBagConstraints();
  165.         c.gridwidth = GridBagConstraints.REMAINDER;
  166.         c.fill = GridBagConstraints.HORIZONTAL;        
  167.         add(textField, c);
  168.  
  169.         c.fill = GridBagConstraints.BOTH;
  170.         c.weightx = 1.0;
  171.         c.weighty = 1.0;
  172.         add(scrollPane, c);  
  173.     }    
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement