Write a Java to Design frame with a Text-box , a text area and a “send” button for chatting application.


import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class ChatFrame extends JFrame implements ActionListener
{
    JTextField t1;
    JButton b1;
    JTextArea ta;
    String newMessage,chat="";

    ChatFrame()
    {
        setLayout(new FlowLayout());
        ta=new JTextArea(10, 50);
        t1=new JTextField(40);
        b1=new JButton("Send");
     
        this.add(ta);
        this.add(t1);
        this.add(b1);

        b1.addActionListener(this);
    }
   
    @Override
    public void actionPerformed(ActionEvent ae)
    {
        String action=b1.getActionCommand();

        if(!action.equals("Send"))
            return;

        newMessage=t1.getText();
        chat=chat+newMessage+"\n";
        ta.setText(chat);
        t1.setText("");
        t1.setFocusable(true);
    }
   
    public static void main(String args[])
    {
        ChatFrame ob=new ChatFrame();
        ob.setVisible(true);
        ob.setSize(100, 150);
    }
}


Comments