?? client_10.26.txt
字號:
//////////////////////////////////////////////////
//Client.java
//071026 11:42 hejia
//////////////////////////////////////////////////
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
//import java.util.*;
import javax.swing.*;
public class Client
{ public static void main(String args[])
{ new ComputerClient();
}
}
class ComputerClient extends Frame implements Runnable,ActionListener,TextListener
{ Button connection,send;
TextArea text1;
TextField inputText,idText;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
MyDialog dialog;
Thread thread;
ComputerClient()
{
socket=new Socket();
setLayout(new FlowLayout());
Box box=Box.createVerticalBox();
//GridLayout grid =new new GridLayout(2,1)
connection=new Button("開始聊天");
send=new Button("發言");
send.setEnabled(false);
text1=new TextArea(10,30);
inputText=new TextField(12);
idText=new TextField(12);
dialog=new MyDialog(this,"ID必須輸入!",true);
//dialog.setVisible(true);
add(text1);
box.add(connection);
box.add(new Label("ID是必須的"));
box.add(idText);
box.add(new Label("注意用語文明"));
box.add(inputText);
box.add(send);
connection.addActionListener(this);
send.addActionListener(this);
text1.addTextListener(this);
thread=new Thread(this);
add(box);
setBounds(80,80,440,230);
setVisible(true);
setResizable(false);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==connection)
{ try //請求和服務器建立套接字連接:
{ if(socket.isConnected())
{}
else
{ InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
send.setEnabled(true);
thread.start();
}
}
catch (IOException ee){}
}
if(e.getSource()==send)
{
String s=inputText.getText();
String ss=idText.getText();
//text1.setText(ss);
if(ss.length()<1){ //用ss==null判斷是不行的,因為SS對象是存在的,要判斷的應是SS中的內容
dialog.setVisible(true); //對話框激活狀態時,堵塞下面的語句
}
else
if(s!=null)
{
try {
out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
public void textValueChanged(TextEvent e){
String s=text1.getText();
}
public void run()
{ String s=null;
while(true)
{ try{ s=in.readUTF();
text1.setText(s);
}
catch(IOException e)
{ text1.setText("與服務器已斷開");
break;
}
}
}
}
class MyDialog extends Dialog implements ActionListener //建立對話框類
{
// static final int YES=1,NO=0;
// int message=-1; Button yes,no;
MyDialog(Frame f,String s,boolean b) //構造方法
{ super(f,s,b);
// yes=new Button("Yes"); yes.addActionListener(this);
// no=new Button("No"); no.addActionListener(this);
// setLayout(new FlowLayout());
// add(yes); add(no);
add(new Label("ID必須輸入!"));
setBounds(160,160,100,100);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ setVisible(false);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
}
/* public void actionPerformed(ActionEvent e)
{ if(e.getSource()==yes)
{ message=YES;setVisible(false);
}
else if(e.getSource()==no)
{ message=NO;setVisible(false);
}
}
public int getMessage()
{ return message;
}
*/
}
//對話中的信息單元;
class Info implements Serializable
//實現Serializable接口
{ String name=null;
//double height;
String words;
Info(String name,String words)
{
this.name=name;
this.word=word;
}
public void setWord (String word)
{ this.word=word;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -