Chat-Server

Màu nền
Font chữ
Font size
Chiều cao dòng

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

import java.io.*;

import java.net.*;

class ServerChatInterface extends JFrame implements ActionListener,KeyListener

{

JMenuBar menu;

JMenu conversation,edit,view,action,help;

//menu items for the file menu

JMenuItem open,close,save,saveas;

//menu items for edit menu

JMenuItem cut,copy,paste;

//menu items and submenus for format menu

JMenuItem right,left,centered,justified,settings;

JMenu font,style,size;

//create a file chooser;

JFileChooser fileChooser;

//create chating areas

JTextArea area1,area2;

//create a button

JButton send;

ServerSocket serverSocket;

Socket socket;

ObjectInputStream is;

ObjectOutputStream os;

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==send)

{

if(area2.getText().length()!=0)

{

try

{

os.writeObject(area2.getText());

area2.setText("");

os.flush();

}

catch(IOException ioe)

{

System.out.println("IOE occurred!");

}

}

}

}

public void keyTyped(KeyEvent ke)

{

if(ke.getKeyCode()==10)

{

ke.consume();

send.doClick();

}

}

public void keyReleased(KeyEvent ke)

{

if(ke.getKeyCode()==10)

{

ke.consume();

send.doClick();

}

}

public void keyPressed(KeyEvent ke)

{

}

public ServerChatInterface()

{

super("Chatting interface");

Container con = getContentPane();

//create a new file chooser

fileChooser = new JFileChooser(new File("MenuBarExample.java"));

//create chating areas

JPanel panel2 = new JPanel();

area1 = new JTextArea(10,10);

area2 = new JTextArea(4,10);

area2.addKeyListener(this);

area1.setBorder(BorderFactory.createLineBorder(Color.BLUE));

area2.setBorder(BorderFactory.createLineBorder(Color.BLUE));

//create the sending button

JPanel buttonPanel = new JPanel();

send = new JButton("Send");

buttonPanel.add(send);

//send.setPreferredSize(new Dimension(20,5));

send.addActionListener(this);

//create a new menu bar

menu = new JMenuBar();

//create menus to add to the above menu bar

conversation = new JMenu("File");

edit = new JMenu("Edit");

view = new JMenu("View");

action = new JMenu("Actions");

help = new JMenu("Help");

//set Mnemonic

conversation.setMnemonic(KeyEvent.VK_C);

edit.setMnemonic(KeyEvent.VK_E);

action.setMnemonic(KeyEvent.VK_A);

view.setMnemonic(KeyEvent.VK_V);

help.setMnemonic(KeyEvent.VK_H);

//create menu items for the file menu

open = new JMenuItem("Open");

open.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

fileChooser.showOpenDialog(null);

}

}

);

close = new JMenuItem("Close");

save = new JMenuItem("Save");

saveas = new JMenuItem("Save as");

saveas.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

fileChooser.showSaveDialog(null);

}

}

);

//create menu items for the edit menu

cut = new JMenuItem("Cut");

copy = new JMenuItem("Copy");

paste = new JMenuItem("Paste");

//create menu items and submenus for the format manu

right = new JMenuItem("Right");

left = new JMenuItem("Left");

centered = new JMenuItem("Centered");

justified = new JMenuItem("Justified");

settings = new JMenuItem("Settings");

font = new JMenu("Font");

size = new JMenu("Size");

style = new JMenu("Style");

//add to file menu

conversation.add(open);

conversation.add(close);

conversation.add(save);

conversation.add(saveas);

//add to edit menu

edit.add(cut);

edit.add(copy);

edit.add(paste);

//add to the menu bar

menu.add(conversation);

menu.add(edit);

menu.add(action);

menu.add(view);

menu.add(help);

this.setJMenuBar(menu);

con.add(area1,"Center");

con.add(buttonPanel,"East");

con.add(area2,"South");

setBounds(300,200,400,300);

setVisible(true);

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

//create a connection

try {

serverSocket =new ServerSocket(2345);

socket=serverSocket.accept();

os=new ObjectOutputStream(socket.getOutputStream());

is=new ObjectInputStream(socket.getInputStream());

do{

String s=(String)is.readObject();

area1.append("Client: "+s+"

");

}while (true);

//os.close();

//socket.close();

}

catch (Exception ex) {ex.printStackTrace();

}

}

public static void main(String[] args)

{

ServerChatInterface frame = new ServerChatInterface();

}

}

Bạn đang đọc truyện trên: Truyen2U.Pro