Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package clientside;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.WindowEvent;
- import java.util.Observable;
- import java.util.Observer;
- import javax.swing.GroupLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.LayoutStyle;
- import javax.swing.WindowConstants;
- import serverside.MerChatLog;
- /**Window representing a 2-way chat, as it appears to one of the participants.
- * Each participant in the chat will have such a window open on their machine.
- *
- * @author Meredith Allen
- */
- public class MerChatWin extends JFrame implements Observer {
- MerChatClient theClient;
- MerChatLog theLog;
- private JButton jButton1;
- private JButton jButton2;
- private JLabel jLabel1;
- private JScrollPane jScrollPane1;
- private JTextArea jTextArea1;
- private JTextField jTextField1;
- public MerChatWin(MerChatClient currentClient,MerChatLog currentLog) {
- this.theClient=currentClient;
- this.theLog=currentLog;
- this.theLog.addObserver(this);
- jButton1 = new JButton();
- jButton2 = new JButton();
- jButton2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println("hit view log.");
- }
- });
- jScrollPane1 = new JScrollPane();
- jTextArea1 = new JTextArea();
- jTextArea1.setEditable(false);
- jTextField1 = new JTextField();
- jTextField1.addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode()==KeyEvent.VK_ENTER)
- {
- MerChatWin.this.jButton1ActionPerformed(null);
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- });
- jLabel1 = new JLabel();
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- jButton1.setText("Send");
- jButton1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("View Log");
- jButton2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- jButton12ActionPerformed(evt);
- }
- });
- jTextArea1.setColumns(20);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- jTextField1.setText("jTextField1");
- jLabel1.setText("Conversation between $User1 and $User2");
- GroupLayout layout = new GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(0, 0, Short.MAX_VALUE)
- .addComponent(jButton1)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton2))
- .addComponent(jScrollPane1)
- .addComponent(jTextField1)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addGap(0, 92, Short.MAX_VALUE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jLabel1)
- .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1)
- .addComponent(jButton2))
- .addContainerGap())
- );
- pack();
- this.jLabel1.setText(String.format("Conversation between %s and %s.",this.theLog.getSelf(),this.theLog.getOtherContact()));
- this.jTextArea1.setText(this.theLog.getLogAsString());
- this.setVisible(true);
- }
- /**Send message buttion action*/
- private void jButton1ActionPerformed(ActionEvent evt) {
- System.out.printf("hit send. Text field text is %s. Text area text is %s.%n",this.jTextField1.getText(),
- this.jTextArea1.getText());
- this.theLog.logMessage(this.theClient, this.jTextField1.getText());
- this.jTextField1.setText("");
- }
- /**View Log button action*/
- private void jButton12ActionPerformed(ActionEvent evt) {
- MerChatLogViewer mclv = new MerChatLogViewer(this.theLog);
- }
- /**Method to facilitate closing the window programmatically*/
- public void closeWindow()
- {
- this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
- }
- /**Observing MerChatLog instance; will update window accordingly*/
- @Override
- public void update(Observable o, Object arg) {
- this.jTextArea1.setText(this.theLog.getLogAsString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment