Guest User

Untitled

a guest
Jan 15th, 2014
4,720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.Panel;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JScrollPane;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JTextField;
  11.  
  12.  
  13. public class Main implements ActionListener {
  14.  
  15.     private static final JFrame frame = new JFrame("Chat Client");
  16.     private static final JTextArea textArea = new JTextArea();
  17.     private static final JTextField textField = new JTextField(25);
  18.     private static final JButton sendButton = new JButton("Send");
  19.    
  20.     public Main() {
  21.         frame.setSize(450, 375);
  22.         frame.setLocationRelativeTo(null);
  23.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.        
  25.         Panel p = new Panel();
  26.        
  27.         sendButton.addActionListener(this);
  28.         textArea.setEditable(false);
  29.         JScrollPane areaScrollPane = new JScrollPane(textArea);
  30.         areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  31.         areaScrollPane.setPreferredSize(new Dimension(430, 275));
  32.        
  33.         p.add(areaScrollPane);
  34.         p.add(textField);
  35.         p.add(sendButton);
  36.        
  37.         frame.add(p);
  38.         frame.setVisible(true);
  39.     }
  40.    
  41.     public static void main(String[] args) {
  42.         new Main();
  43.     }
  44.  
  45.     @Override
  46.     public void actionPerformed(ActionEvent arg0) {
  47.         String message = textField.getText();
  48.         textArea.append(message + "\n");
  49.         textField.setText("");
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment