📄 mp_client.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class MP_Client extends JFrame implements ActionListener{
JLabel mpLabel = new JLabel("My phone number: ");
JLabel mpnLabel = new JLabel("");
JLabel opnLabel = new JLabel("Opposite phone number: ");
JLabel msgLabel = new JLabel("Message:");
JTextField opn = new JTextField(18);
JTextArea message = new JTextArea(6,18);
JScrollPane scroll = new JScrollPane(message,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JButton bsend = new JButton("Send");
JButton breceive = new JButton("Receive");
JButton bquit = new JButton("Quit");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6= new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b0 = new JButton("0");
String mypnumber=null;
public String mypn;
public File data;
public Socket socket = null;
public ObjectOutputStream os = null;
public ObjectInputStream is = null;
public MP_Client() {
super ("My Phone");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300,225,240,380);
JPanel pane = new JPanel();
String mypn = JOptionPane.showInputDialog(this, "Please input a phone number you want:",
"Set your phone number", JOptionPane.QUESTION_MESSAGE);
mypnumber = mypn;
pane.add(mpLabel);
pane.add(mpnLabel);
mpnLabel.setText(mypn);
pane.add(opnLabel);
pane.add(opn);
pane.add(msgLabel);
pane.add(scroll);
//pane.add(scrollts);
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b4);
pane.add(b5);
pane.add(b6);
pane.add(b7);
pane.add(b8);
pane.add(b9);
pane.add(b0);
pane.add(breceive);
pane.add(bsend);
pane.add(bquit);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
breceive.addActionListener(this);
bsend.addActionListener(this);
bquit.addActionListener(this);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getSource().equals(b1))
{
opn.setText(opn.getText()+"1");
}
if (e.getSource().equals(b2))
{
opn.setText(opn.getText()+"2");
}
if (e.getSource().equals(b3))
{
opn.setText(opn.getText()+"3");
}
if (e.getSource().equals(b4))
{
opn.setText(opn.getText()+"4");
}
if (e.getSource().equals(b5))
{
opn.setText(opn.getText()+"5");
}
if (e.getSource().equals(b6))
{
opn.setText(opn.getText()+"6");
}
if (e.getSource().equals(b7))
{
opn.setText(opn.getText()+"7");
}
if (e.getSource().equals(b8))
{
opn.setText(opn.getText()+"8");
}
if (e.getSource().equals(b9))
{
opn.setText(opn.getText()+"9");
}
if (e.getSource().equals(b0))
{
opn.setText(opn.getText()+"0");
}
if (e.getSource().equals(bsend))
{
Message sendmessage =new Message(this.mpnLabel.getText(),this.opn.getText(),
this.message.getText());
this.send(sendmessage);
}
if (e.getSource().equals(breceive))
{
Message receivemessage = new Message(this.mpnLabel.getText(),"0000","receive");
this.send(receivemessage);
}
if (e.getSource().equals(bquit))
{
Message closemessage = new Message(this.mpnLabel.getText(),"0000","closephone");
this.send(closemessage);
System.exit(0);
}
}
// the constructor expects the IP address of the server - the port is fixed at 5050
public void ThePhoneClient (String serverIP)
{
if (!connectToServer(serverIP))
{
message.setText("Cannot open socket connection...");
}
}
private boolean connectToServer(String serverIP)
{
try // open a new socket to port: 5050 and create streams
{
this.socket = new Socket(serverIP,5050);
this.os = new ObjectOutputStream(this.socket.getOutputStream());
this.is = new ObjectInputStream(this.socket.getInputStream());
Message fristmessage = new Message(mypnumber,"0000","sendnumber@");
try
{
this.send(fristmessage);
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
catch (Exception ex)
{
return false;
}
return true;
}
public void send (Object o)
{
try
{
this.os.writeObject(o);
this.os.flush();
}
catch (Exception ex)
{
message.setText("Cannot open socket connection\n"+ ex.toString());
}
}
public Object receive()
{
Object o = null;
try
{
o = is.readObject();
}
catch (Exception ex)
{
message.setText("Cannot open socket connection\n"+ ex.toString());
}
return o;
}
public void close()
{
Message closemessage = new Message(mypn,"0000","closephone");
this.send(closemessage);
System.exit(0);
}
public static void main(String[] arguments){
MP_Client sf = new MP_Client();
sf.ThePhoneClient("127.0.0.1");
sf.getContentPane().setBackground(Color.ORANGE);
while(true)
{
Message amessage =new Message(null,null,null);
try
{
amessage = (Message)sf.is.readObject();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);//记着加处理 出错后关闭它
}
sf.opn.setText(amessage.getmynumber());
sf.message.setText(amessage.getMessagetext());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -