import java.awt.*;
import java.awt.event.*;
/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SockFrm extends JFrame{
private JTextArea Txtaa = new JTextArea(4,20);
private JTextField Txtfd = new JTextField(20);
private JButton JBon = new JButton("发送");
private String IPstr;
private SockFrm SF;
private Socket soc=null;
private PrintStream out=null;
private BufferedReader in;
private BufferedReader stdIn;
public SockFrm(){
super("网络聊天实例");
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("聊天记录:");
JLabel lab2 = new JLabel("你的发言:");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu FileMenu = new JMenu("文件");
JMenuItem ConSerMenu = new JMenuItem("连接服务器");
ConSerMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent Event){
IPFrame IPF = new IPFrame();
IPF.setParent(SF);
//IPF.setDefaultCloseOperation(IPFrame.EXIT_ON_CLOSE);
}
});
FileMenu.add(ConSerMenu);
bar.add(FileMenu);
cont.add(lab1);
cont.add(Txtaa);
cont.add(lab2);
cont.add(Txtfd);
cont.add(JBon);
setSize(280,260);
setVisible(true);
SF=this;
JBon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
out.println(Txtfd.getText()+"\n");
out.flush();
}
});
}
public void ConServer(String IP){
String inputLine;
IPstr=IP;
int Num=0;
//JOptionPane.showMessageDialog(null,IPstr,"系统提示",JOptionPane.INFORMATION_MESSAGE);
try{
soc = new Socket(IPstr,9494);
out = new PrintStream(soc.getOutputStream());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//stdIn = new BufferedReader(new InputStreamReader(System.in));
while((Num=in.read())!=0){
char[] cBuf = new char[Num];
in.read(cBuf);
inputLine= new String(cBuf);
Txtaa.setText(Txtaa.getText()+inputLine);
}
}catch (IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
SockFrm SF= new SockFrm();
SF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class IPFrame extends JFrame{
private String IPadd;
private JTextField JTF = new JTextField(15);
private JButton JBon1 = new JButton("确定");
private JButton JBon2 =new JButton("取消");
private SockFrm isPa;
public IPFrame(){
super("输入IP地址");
Container Cont = getContentPane();
Cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("服务器IP:");
Cont.add(lab1);
Cont.add(JTF);
Cont.add(JBon1);
Cont.add(JBon2);
ButtonHandle handle = new ButtonHandle();
JBon1.addActionListener(handle);
JBon2.addActionListener(handle);
setSize(300,100);
setVisible(true);
}
public void setParent(SockFrm SF){
isPa = SF;
}
private class ButtonHandle implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource()==JBon1){
IPadd = JTF.getText();
dispose();
isPa.ConServer(IPadd);
}else if(event.getSource()==JBon2){
dispose();
}