📄 myclient.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyClient extends Frame implements ActionListener
{
Socket ClientSocket;
PrintStream os;
DataInputStream is;
String s;
Label Mylabel=new Label("欢迎使用本机提供的信息");
Label enter1=new Label("x=");
Label enter2=new Label("y=");
TextField enterx=new TextField(0);
TextField entery=new TextField(0);
TextArea textArea;
Button Line=new Button("连接");
Button Open=new Button("断开");
Button MyButton=new Button("发送");
public MyClient()
{
setTitle("Client Window");
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gbl);
this.addWindowListener(new WinAdptClient(this));
textArea=new TextArea(20,50);
gbc.gridx=1;gbc.gridy=1;
gbc.gridwidth=4;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(Mylabel,gbc);
add(Mylabel);
gbc.gridx=1;gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.anchor=GridBagConstraints.WEST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(enter1,gbc);
add(enter1);
gbc.gridx=3;gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.anchor=GridBagConstraints.WEST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(enter2,gbc);
add(enter2);
gbc.gridx=2;gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.anchor=GridBagConstraints.SOUTHEAST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(enterx,gbc);
add(enterx);
gbc.gridx=4;gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.anchor=GridBagConstraints.SOUTHEAST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(entery,gbc);
add(entery);
gbc.gridx=1;gbc.gridy=3;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(MyButton,gbc);
MyButton.addActionListener(this);
add(MyButton);
gbc.gridx=3;gbc.gridy=3;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(Line,gbc);
Line.addActionListener(this);
add(Line);
gbc.gridx=4;gbc.gridy=3;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(Open,gbc);
Open.addActionListener(this);
add(Open);
gbc.gridx=1;gbc.gridy=4;
gbc.gridwidth=4;gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(textArea,gbc);
add(textArea);
setResizable(false);
pack();
show();
//connect();
}
public void connect()
{
try{
ClientSocket=new Socket(InetAddress.getLocalHost(),8000);
os=new PrintStream(
new BufferedOutputStream(ClientSocket.getOutputStream()));
is=new DataInputStream(
new BufferedInputStream(ClientSocket.getInputStream()));
s=is.readLine();
textArea.appendText(s+"\n");
}
catch(Exception e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==MyButton)
{
try{
os.println(enterx.getText());
os.println(entery.getText());
os.flush();
}
catch(Exception a){}
try
{
is=new DataInputStream(
new BufferedInputStream(ClientSocket.getInputStream()));
s=is.readLine();
textArea.appendText(s+"\n");
}
catch(Exception a){}
}
else if(e.getActionCommand()=="连接")
{
connect();
}
else if(e.getActionCommand()=="断开")
{
try{
is.close();
os.close();
ClientSocket.close();
}
catch(Exception a){}
}
}
public static void main(String args[])
{
new MyClient();
}
}
class WinAdptClient extends WindowAdapter
{
MyClient m_Parent;
WinAdptClient(MyClient p)
{
m_Parent=p;
}
public void windowClosing(WindowEvent e)
{
try{
m_Parent.is.close();
m_Parent.os.close();
m_Parent.ClientSocket.close();
m_Parent.dispose();
System.exit(0);
}catch(Exception a){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -