Commit ec1b977d authored by armin's avatar armin

final

parents
import javax.swing.*;
import java.awt.*;
public class ChatArea extends JTextArea {
private static final int ROWS = 20 , COLUMNS = 40;
public ChatArea()
{
super(ROWS, COLUMNS);
this.setEditable(false);
this.setLineWrap(true);
setVisible(true);
this.setBackground(Color.cyan);
}
public void addMessage(String username,String message)
{
this.append(username+": "+message+"\n");
}
}
\ No newline at end of file
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class ChatRoomGUI extends JFrame {
private final String WINDOWS_TITLE = "AUT Chat Room";
private final int WIDTH = 700 , HEIGHT = 700;
private final int X = 150 , Y = 150;
private ArrayList<String> users = new ArrayList<>();
ParticipantsArea participantsArea = new ParticipantsArea();
MessageArea messageArea = new MessageArea();
ChatArea chatBox = new ChatArea();
public ChatRoomGUI() {
super();
this.setFont(new Font("serif", Font.PLAIN, 40));
this.setTitle(WINDOWS_TITLE);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setLocation(X, Y);
this.add(new JScrollPane(chatBox), BorderLayout.CENTER);
this.add(participantsArea, BorderLayout.WEST);
this.add(messageArea, BorderLayout.SOUTH);
this.setVisible(true);
participantsArea.addPart("armin");
participantsArea.addPart("arta");
participantsArea.addPart("aava");
participantsArea.addPart("ali");
participantsArea.addPart("reza");
participantsArea.removePart("ali");
}
public ChatArea getChatBox() {
return chatBox;
}
public MessageArea getMessageArea() {
return messageArea;
}
public void showGUI() {
this.setVisible(true);
}
public void addNewPart(String user) {
users.add(user);
participantsArea.addPart(user);
}
public void removePart(String user) {
try
{
users.remove(user);
} catch (Exception e)
{
}
}
}
\ No newline at end of file
public class Main {
public static void main(String[] args) {
UsernameFrame usernameFrame1 = new UsernameFrame(1);
usernameFrame1 = new UsernameFrame("a");
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MessageArea extends JPanel
{
private static final String BTN_TXT = " Send Message ";
JTextField textField;
JButton btn;
public MessageArea() throws HeadlessException {
super();
this.setLayout(new BorderLayout());
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton(BTN_TXT);
add(btn, BorderLayout.EAST);
setVisible(true);
this.setBackground(Color.gray);
}
public void setTextField(String textField) {
this.textField.setText(textField);
}
public JButton getBtn() {
return btn;
}
public JTextField getTextField() {
return textField;
}
}
import javax.swing.*;
import java.awt.*;
public class ParticipantsArea extends JPanel
{
private static final String LABEL_TXT = "Online People:";
private static int count = 0;
DefaultListModel model = new DefaultListModel();
public ParticipantsArea() throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel(LABEL_TXT);
JList list = new JList(model);
add(label, BorderLayout.NORTH);
add(list, BorderLayout.CENTER);
setVisible(true);
}
public void addPart(String user)
{
model.addElement(user) ;
}
public void removePart(String user)
{
model.removeElement(user) ;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class RemoveFrame extends JFrame {
private static final String BTN_TXT = " Start Chatting ...";
private static final String LABEL_TXT = " Choose Your UserName ";
private static final int WIDTH = 300, HEIGHT = 200;
public static ChatRoomGUI mainChatRoom;
JTextField textField;
JButton btn;
public String userName;
public static ChatRoomGUI getMainChatRoom() {
return mainChatRoom;
}
public RemoveFrame() throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel("Choose Your UserName");
add(label, BorderLayout.PAGE_START);
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton("Enter Your UserName");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
userName=textField.getText();
setVisible(false);
mainChatRoom = new ChatRoomGUI();
mainChatRoom.removePart(userName);
JTextField txtField = mainChatRoom.getMessageArea().getTextField();
txtField.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
if(evt.getKeyCode() == KeyEvent.VK_ENTER && !evt.isShiftDown())
{
String txt = mainChatRoom.getMessageArea().getTextField().getText();
mainChatRoom.getChatBox().addMessage(userName,txt);
mainChatRoom.getMessageArea().setTextField("");
}
else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
txtField.setText(txtField.getText()+'\n');
else
txtField.setText(txtField.getText()+evt.getKeyChar());
}
});
JButton btn = mainChatRoom.getMessageArea().getBtn();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String txt = mainChatRoom.getMessageArea().getTextField().getText();
mainChatRoom.getChatBox().addMessage(userName,txt);
mainChatRoom.getMessageArea().setTextField("");
}
});
}
});
add(btn, BorderLayout.PAGE_END);
setSize(WIDTH, HEIGHT);
setVisible(true);
this.setBackground(Color.green);
}
public String getUserName() {
return userName;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class UsernameFrame extends JFrame {
private static final String BTN_TXT = " Start Chatting ...";
private static final String LABEL_TXT = " Choose Your UserName ";
private static final int WIDTH = 300, HEIGHT = 200;
public static ChatRoomGUI mainChatRoom;
JTextField textField;
JButton btn;
public String userName;
public static ChatRoomGUI getMainChatRoom() {
return mainChatRoom;
}
public UsernameFrame(String b) throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel("Choose Your UserName");
add(label, BorderLayout.PAGE_START);
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton("Enter Your UserName");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
userName=textField.getText();
setVisible(false);
mainChatRoom = new ChatRoomGUI();
mainChatRoom.addNewPart(userName);
JTextField txtField = mainChatRoom.getMessageArea().getTextField();
txtField.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
if(evt.getKeyCode() == KeyEvent.VK_ENTER && !evt.isShiftDown())
{
String txt = mainChatRoom.getMessageArea().getTextField().getText();
mainChatRoom.getChatBox().addMessage(userName,txt);
mainChatRoom.getMessageArea().setTextField("");
}
else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
txtField.setText(txtField.getText()+'\n');
else
txtField.setText(txtField.getText()+evt.getKeyChar());
}
});
JButton btn = mainChatRoom.getMessageArea().getBtn();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String txt = mainChatRoom.getMessageArea().getTextField().getText();
mainChatRoom.getChatBox().addMessage(userName,txt);
mainChatRoom.getMessageArea().setTextField("");
}
});
}
});
add(btn, BorderLayout.PAGE_END);
setSize(WIDTH, HEIGHT);
setVisible(true);
this.setBackground(Color.green);
}
public UsernameFrame(int a) throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel("Choose Your UserName");
add(label, BorderLayout.PAGE_START);
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton("Enter Your UserName");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
userName=textField.getText();
setVisible(false);
mainChatRoom = new ChatRoomGUI();
mainChatRoom.removePart(userName);
JTextField txtField = mainChatRoom.getMessageArea().getTextField();
txtField.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
if(evt.getKeyCode() == KeyEvent.VK_ENTER && !evt.isShiftDown())
{
String txt = mainChatRoom.getMessageArea().getTextField().getText();
mainChatRoom.getChatBox().addMessage(userName,txt);
mainChatRoom.getMessageArea().setTextField("");
}
else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
txtField.setText(txtField.getText()+'\n');
else
txtField.setText(txtField.getText()+evt.getKeyChar());
}
});
JButton btn = mainChatRoom.getMessageArea().getBtn();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String txt = mainChatRoom.getMessageArea().getTextField().getText();
mainChatRoom.getChatBox().addMessage(userName,txt);
mainChatRoom.getMessageArea().setTextField("");
}
});
}
});
add(btn, BorderLayout.PAGE_END);
setSize(WIDTH, HEIGHT);
setVisible(true);
this.setBackground(Color.green);
}
public String getUserName() {
return userName;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment