📄 echoclientgui1.java
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class EchoClientGUI1 implements MouseListener,WindowListener
{
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
TextField t = null;
TextField t2 = null;
public static void main(String[] args) throws IOException
{
EchoClientGUI1 self = new EchoClientGUI1();
self.go(self);
}
public void go(EchoClientGUI1 self) throws IOException
{
//实例一个容器组件
Frame f = new Frame( "Example" );
//设置边界大小
f.setBounds( 0,0,300,300 );
//实例化一个网格组布局管理器
GridBagLayout gridbag = new GridBagLayout();
//实例化一个网格组布局管理器的辅助限制类
GridBagConstraints c = new GridBagConstraints();
//设置布局管理器
f.setLayout(gridbag );
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.0;
c.gridwidth = GridBagConstraints.RELATIVE;
Button b = new Button( "send" );
gridbag.setConstraints( b, c );
f.add( b,c );
c.weightx = 1.0;
t = new TextField( "write some thing to send" );
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints( t, c );
f.add( t,c );
c.weightx = 1.0;
t2 = new TextField();
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints( t2, c );
f.add( t2,c );
try
{
echoSocket = new Socket ( "localhost", 1111);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: localhost.");
System.exit(1);
}
b.addMouseListener(self);
f.addWindowListener(self);
f.setVisible( true );
in.readLine();
in.readLine();
}
//button的鼠标事件
public void mouseClicked(MouseEvent e)
{
try
{
out.println(t.getText());
t2.setText(in.readLine()) ;
}catch(IOException ie){}
}
//关闭窗口
public void windowClosing(WindowEvent e)
{
try
{
out.close();
in.close();
echoSocket.close();
System.exit(0);
}catch(IOException ie){}
}
//实现接口中的方法
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -