📄 mclient.java
字号:
/****************************
Created by : Khurram Nawaz Abbasi
Student ID : 600304568
****************************/
//Different Packages introduced under these pakages i worked on my program below
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.*;
// Client Class start from here
public class MClient extends JFrame implements Runnable, WindowListener, ActionListener
{
protected JMenu clientMenu, editMenu, actionMenu, toolMenu;
protected int port = 1234; // port number on which server will listen
protected JTextArea output;// Text area where the input messages will be displayed
protected JTextField input;// Text field where client will enter message
protected JButton send;// button that will be used to send message
protected JPanel topPanel, centerPanel, bottomPanel;
protected DataInputStream dataIn;// Used to read data on the port
protected DataOutputStream dataOut;// used to send the data
protected Thread listener;
protected String host;
protected JMenuItem Save;
protected JLabel statusBar;
protected String userName = "Client";
protected Color col;
public MClient(int port)
{// constructor starts from here
super("Client- Conversation");// this will display information on the title bar
userName = JOptionPane.showInputDialog(
MClient.this, "Enter user name:");
this.port = port;// refers to current port
}// constructor ends here
/****************************************** start method starts here ***************************************************/
public synchronized void start() throws IOException
{
if (listener == null)
{// if starts here
Socket socket = new Socket(host, port);// to create a socket
try
{// try block start here
dataIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
dataOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
catch (IOException ioex)
{// catch block starts here
socket.close();
throw ioex;
}
listener = new Thread(this);
listener.start();
GUI();// calling the graphical user interface method
}// end if
}// start() method ends here
/****************************************** start method ends here ***************************************************/
/****************************************** stop method starts here ***************************************************/
public synchronized void stop() throws IOException
{
hide();
if (listener != null)
{
listener.interrupt();
listener = null;
dataOut.close();
}
}
public void run()
{
try
{
while (!Thread.interrupted())
{
String line = dataIn.readUTF();
output.append(line + "\n");
}
}
catch (IOException ex)
{
handleIOException(ex);
}
}
/****************************************** stop method ends here ***************************************************/
/****************************************** GUI method starts here ***************************************************/
protected void GUI()
{
Random r = new Random();
col = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
sdf.setTimeZone(TimeZone.getDefault());
input = new JTextField(30);
input.addActionListener(this);
output = new JTextArea(15, 30);
send = new JButton("Send");
//input.setColor(Color.blue);
input.setForeground(col);
output.setForeground(col);
input.setFont(new Font("Serif", Font.PLAIN, 15));
output.setFont(new Font("Courier", Font.PLAIN, 15));
bottomPanel = new JPanel();
output.setEditable(false);
clientMenu = new JMenu("File"); // create File JMenu
clientMenu.setMnemonic('F'); // set mnemonic for server menu
editMenu = new JMenu("Edit"); // create Edit JMenu
editMenu.setMnemonic('E'); // set mnemonic for server menu
actionMenu = new JMenu("Action"); // create Action JMenu
actionMenu.setMnemonic('A'); // set mnemonic for server menu
toolMenu = new JMenu("Tools"); // create Tool JMenu
toolMenu.setMnemonic('T'); // set mnemonic for server menu
JMenuBar menuBar = new JMenuBar(); // create JMenuBar
menuBar.add(clientMenu); // add server menu to menu bar
menuBar.add(editMenu); // add server menu to menu bar
menuBar.add(actionMenu); // add server menu to menu bar
menuBar.add(toolMenu); // add server menu to menu bar
setJMenuBar(menuBar); // add JMenuBar to application
Save = new JMenuItem("Save");
Save.setMnemonic('S');
clientMenu.add(Save);
Box box = new Box(BoxLayout.X_AXIS);
box.add(new JScrollPane(input)); // add input area to box
box.add(send); // add send button to box
Container c = getContentPane();
c.add(new JScrollPane(output), BorderLayout.NORTH);
statusBar = new JLabel(userName + ": Conn... at " + sdf.format(cal.getTime()));
//statusBar.setBorder( new BevelBorder( BevelBorder.LOWERED ) );
bottomPanel.add(box, BorderLayout.CENTER);
//bottomPanel.add( statusBar, BorderLayout.SOUTH);
c.add(bottomPanel, BorderLayout.CENTER);
c.add(statusBar, BorderLayout.SOUTH);
//c.add(send, BorderLayout.SOUTH);
/***************************************************************/
/*send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
sendData(e.getActionCommand());
}
}); */
/******************************************************/
addWindowListener(this);
pack();
show();
}
/****************************************** GUI() ends here ***************************************************/
protected synchronized void handleIOException(IOException ex)
{
if (listener != null)
{
output.append(ex + "\n");
input.setVisible(false);
validate();
if (listener != Thread.currentThread())
listener.interrupt();
listener = null;
try
{
dataOut.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/***********************************/
private void sendData(String s)
{
//try
// {
String message = s;
// output.writeObject("CLIENT>> :"+ s);
// output.flush();
output.append("CLIENT>>>>>" + s);
/* }
catch(IOException cnfex)
{
messageArea.append("\n Error Writing Object");
} */
}
/******************************/
public void windowOpened(WindowEvent event)
{
input.requestFocus();
}
public void windowClosing(WindowEvent event)
{
try
{
stop();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public void windowClosed(WindowEvent event) { }
public void windowIconified(WindowEvent event) { }
public void windowDeiconified(WindowEvent event) { }
public void windowActivated(WindowEvent event) { }
public void windowDeactivated(WindowEvent event) { }
/******************************************
Action listener to send message
***************************************************/
public void actionPerformed(ActionEvent e)
{
try
{
input.selectAll();
dataOut.writeUTF(userName + " says: " +
e.getActionCommand());
dataOut.flush();
input.setText("");
}
catch (IOException ex)
{
handleIOException(ex);
}
}//Action ends here
/******************************************
Main() method starts here
*******************************************/
public static void main(String[] args) throws IOException
{
int port = 1234;
/***************************************/
/*****************************************/
MClient client = new MClient(port);// creating object of MClient class
client.start();
}// main ends here
}// MClient class ends here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -