📄 tcp_frame.java
字号:
import java.awt.AWTEvent;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class TCP_Frame extends JFrame implements Runnable
{
public final static int TCP_PORT = 6321;
public Socket client;
Thread th;
JPanel contentPane;
JTextField txtInput = new JTextField();
JButton TCPSend = new JButton();
List lstMsg = new List();
JLabel Back = new JLabel();
protected BufferedReader in;
protected PrintWriter out;
String id;
public TCP_Frame(Socket socket,String nickname)//构造函数
{
Ring(3); //播放提示音
id = new String(nickname);
client = socket;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
TCP_init(nickname);//初始化界面
startConnect();//连接终端
}
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
try
{
sendMsg("User have been disconnect!");
in.close();
out.close();
client.close();
}catch(IOException ek)
{
ek.printStackTrace();
}
}
}
public void TCP_init(String nickname)
{
Icon BG = new ImageIcon("image\\BackGround.gif");
Icon sentIcon = new ImageIcon("image\\BTSend.gif");
Back = new JLabel();
Back.setIcon(BG);
Back.setBounds(0,0,350,185);
String name = new String(nickname);
txtInput.setText("");
txtInput.setBounds(new Rectangle(20,140,200,30));
txtInput.addKeyListener(new KeyAdapter (){
public void keyPressed(KeyEvent ek){
if(ek.getKeyChar() == '\n'){
try
{
sendMsg(txtInput.getText());
processMsg("Me:"+txtInput.getText());
txtInput.setText("");
}catch(IOException e2)
{
processMsg(e2.toString());
}
}
}
});
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(350,210);
this.setTitle("Talk to "+name);
TCPSend.setIcon(sentIcon);
TCPSend.setBounds(new Rectangle(240,140,89,32));
TCPSend.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent es)
{
TCPSend_actionPerformed(es);
}
});
lstMsg.setBounds(new Rectangle(20,35,300,100));
contentPane.add(txtInput,null);
contentPane.add(TCPSend,null);
contentPane.add(lstMsg,null);
contentPane.add(Back,null);
this.setResizable(false);
}
/*
* 播放提示音,声音文件使用流文件导入,并通过sun.audio.*对声音文件进行播放
*/
public void Ring(int i)
{
FileInputStream file;
try {
switch(i)
{
case 1:
file=new FileInputStream("sound\\ahoh.wav");
break;
case 2:
file=new FileInputStream("sound\\(w)goodbye.wav");
break;
case 3:
file=new FileInputStream("sound\\hello.wav");
break;
default:
file=new FileInputStream("ahoh.wav");
}
AudioStream as=new AudioStream(file);
AudioPlayer.player.start(as);
}
catch (Exception e) {}
}
public void startConnect()//连接终端函数
{
try
{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
processMsg("连接成功");
out.println("-------------------------------------");
out.flush();
}catch(IOException Te)
{
Te.printStackTrace();
processMsg("连接失败");
}
if(th == null)
{
th = new Thread(this);
th.start();
}
}
public void run()//run()实现了对TCP端口的监听及处理
{
String msg = null;
try
{
msg = receiveMsg();
while(!msg.equals("User have been disconnect!"))//当对方终端退出时会发送"User have been disconnect!",这是结束循环,关闭端口
{
try
{
msg = receiveMsg();
th.sleep(100L);
if(msg != null&&!msg.equals("User have been disconnect!"))
{
Ring(1);
processMsg(id+":"+msg);
}
}catch(IOException w)
{
in.close();
out.close();
client.close();
msg = new String("User have been disconnect!");
}
}
Ring(2);
in.close();
out.close();
client.close();
this.setVisible(false);
this.dispose();
}catch(IOException Te2)
{
try
{
in.close();
out.close();
client.close();
}
catch(IOException ee)
{
ee.printStackTrace();
}
Te2.printStackTrace();
}catch(InterruptedException e1){}
finally
{
try
{
in.close();
out.close();
client.close();
}
catch(IOException ee)
{
ee.printStackTrace();
}
}
}
public String receiveMsg()throws IOException//接收数据
{
String msg = new String();
try
{
msg = in.readLine();
}catch(IOException e)
{
e.printStackTrace();
in.close();
out.close();
client.close();
msg = new String("User have been disconnect!");
}
return msg;
}
public void sendMsg(String str)throws IOException//发送数据
{
out.println(str);
out.flush();
}
public void processMsg(String str)//显示信息在会话框中
{
this.lstMsg.add(str);
this.lstMsg.select(lstMsg.getItemCount()-1);
}
void TCPSend_actionPerformed(ActionEvent e)//发送按钮事件
{
try
{
sendMsg(txtInput.getText());
processMsg("Me:"+txtInput.getText());
this.txtInput.setText("");
}catch(IOException e2)
{
processMsg(e2.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -