📄 talkclient.java
字号:
package Talk;
import java.net.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import useor.User;
//TERMINATE
public class TalkClient extends JFrame{
private String message = "";
private JTextArea senttextArea;
private JTextArea showtextArea;
private Socket client;
private ObjectOutputStream output;
private ObjectInputStream input;
private String chatServer;
private String talkto;
private User u;
/**
*
*
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
TalkClient application;
if ( args.length == 0 )
application = new TalkClient( "127.0.0.1" );
else
application = new TalkClient( args[ 0 ] );//?
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
application.runClient();
}
public TalkClient(String host)//public TalkClient(String host,User u)
{
super("客户端");
this.setSize(500, 420);
getContentPane().setLayout(null);
chatServer=host;
u=new User();
u.setname("hi");//设置谈话对象
talkto=u.getname();
final JPanel showpanl = new JPanel();
showpanl.setBorder(new TitledBorder(null, "与 "+talkto+" 对话", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
showpanl.setLayout(new BorderLayout());
showpanl.setBounds(14, 16, 468, 235);
getContentPane().add(showpanl);
showtextArea = new JTextArea();
showtextArea.setBorder(new BevelBorder(BevelBorder.LOWERED));
showpanl.add(showtextArea, BorderLayout.CENTER);
final JPanel sentpanl = new JPanel();
sentpanl.setBorder(new TitledBorder(null, "输入", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
sentpanl.setLayout(new BorderLayout());
sentpanl.setBounds(13, 269, 469, 72);
getContentPane().add(sentpanl);
senttextArea = new JTextArea();
senttextArea.setBorder(new BevelBorder(BevelBorder.LOWERED));
senttextArea.setEnabled(false);
sentpanl.add(senttextArea, BorderLayout.CENTER);
final JButton sentbutton = new JButton();
sentbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message=senttextArea.getText();
sentmessage(message);
System.out.println(message);
}
});
sentbutton.setText("发送");
sentbutton.setBounds(383, 350, 99, 23);
getContentPane().add(sentbutton);
this.setVisible(true);
this.setResizable(false);
final JButton off = new JButton();
off.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
sentmessage("TERMINATE");//TERMINATE
}
});
off.setText("关闭");
off.setBounds(280, 350, 99, 23);
getContentPane().add(off);
}
public void getstream()
{
try
{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
showtextArea.append( "\nGot I/O streams\n" );
// showtextArea.setText(input.toString());
}
catch(IOException e){}
}
public void sentmessage(String message)
{
try
{
output.writeObject("CLIENT>>>"+message);
output.flush();
showtextArea.append("\nCLIENT>>>"+message);
}
catch(Exception e){}
}
public void getconnection()
{
try{
showtextArea.setText("connecting");
client =new Socket(InetAddress.getByName(chatServer),5000);
showtextArea.append("Connected to: " +
client.getInetAddress().getHostName());
}
catch (Exception e){}
}
private void processConnection() throws IOException
{
senttextArea.setEnabled( true );
do {
try {
message = ( String ) input.readObject();
showtextArea.append( "\n" + message );
showtextArea.setCaretPosition(
showtextArea.getText().length() );
}
// server 问题
catch ( ClassNotFoundException classNotFoundException ) {
showtextArea.append( "\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>>TERMINATE" ) );
}
private void closeConnection() throws IOException
{
showtextArea.append( "\n"+"Closing connection" );
output.close();
input.close();
client.close();
}
public void runClient()
{
try {
getconnection();
getstream();
processConnection();
closeConnection();
}
catch ( EOFException eofException ) {
System.out.println( "Server terminated connection" );
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
}
/*try
{
Socket socket=new Socket("127.0.0.1",4700);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline=sin.readLine();
while(!readline.equals("bye"))//end with bye
{
os.println(readline);
os.flush();//刷新 服务器马上接收到
System.out.println("Client:"+readline);
System.out.println("Server:"+is.readLine());
//从服务器读入 打印
readline=sin.readLine();
if (readline==null) break;
}
os.close();
is.close();
socket.close();
sin.close();
}
catch(Exception e)
{System.out.println("error"+e.getMessage());}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -