|
[PHP]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class NetBox extends JFrame {
JFrame parent;
String lang1="English";
String lang2="Chinese";
String sermodes="服务器模式";
String climodes="客户机模式";
String hosts="主机";
String ports="端口";
String connects="连接";
String listens="监听";
String stops="断开";
String defaultip="192.168.32.57";
String defaultport="1001";
String infos="状态:";
String closes="关闭";
String sends="发送";
MyThread ListenThread;
JRadioButton sermode=new JRadioButton(sermodes);
JRadioButton climode=new JRadioButton(climodes);
JLabel hostl=new JLabel(hosts);
JTextField host=new JTextField(28);
JLabel portl=new JLabel(ports);
JTextField port=new JTextField(defaultport,6);
JButton connect=new JButton(connects);
JButton stop=new JButton(stops);
JLabel infol=new JLabel(infos);
JTextField info=new JTextField(39);
ButtonGroup group=new ButtonGroup();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JTabbedPane tabs=new JTabbedPane();
private class MyThread extends Thread{
public void close(){};
}
private JPanel CreateTab(final JTabbedPane parentpane,final Socket socket){
final Thread t;
final PrintWriter out;
final BufferedReader in;
final JTextArea history;
final JButton send;
final JTextField msg;
final JPanel panel;
final JButton close;
final JScrollPane historypane;
final JLabel myinfol;
final JTextField myinfo;
final JPanel panel1;
final JPanel panel2;
try{
out=new PrintWriter(socket.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(Exception e){
info.setText(e.getMessage());
return null;
}
panel=new JPanel(new BorderLayout());
msg=new JTextField(34);
send=new JButton(sends);
history=new JTextArea(6,30);
history.setEditable(false);
historypane=new JScrollPane(history);
close=new JButton(closes);
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent ev){
out.println(msg.getText());
history.append("Send: "+msg.getText()+"\n");
msg.setText("");
}
};
msg.addActionListener(al);
send.addActionListener(al);
myinfol=new JLabel(infos);
myinfo=new JTextField(30);
myinfo.setEditable(false);
t=new Thread(){
public void run(){
String s;
try{
while(true){
s=in.readLine();
if(s==null){
myinfo.setText("Socket closed.");
msg.setEnabled(false);
send.setEnabled(false);
return;
}
history.append("Receive:"+s+"\n");
}
}catch(Exception e){
myinfo.setText(e.getMessage());
msg.setEnabled(false);
send.setEnabled(false);
}
}
};
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
try{
// t.destroy();
socket.close();
in.close();
out.close();
}catch(Exception e1){
info.setText(e1.getMessage());
}
parentpane.remove(panel);
parent.pack();
}
});
t.start();
panel1=new JPanel();
panel1.add(msg);
panel1.add(send);
panel2=new JPanel();
panel2.add(myinfol);
panel2.add(myinfo);
panel2.add(close);
panel.add(panel1,BorderLayout.NORTH);
panel.add(historypane,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);
return panel;
}
public NetBox(){
parent=this;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));
group.add(sermode);
group.add(climode);
panel1.add(climode);
climode.setSelected(true);
stop.setEnabled(false);
panel1.add(sermode);
panel2.add(hostl);
info.setEditable(false);
try{
String s=(InetAddress.getLocalHost()).getHostAddress();
defaultip=s;
}catch(Exception e){
info.setText("Can not get local address");
}
host.setText(defaultip);
panel2.add(host);
panel2.add(portl);
panel2.add(port);
panel3.add(connect);
panel3.add(stop); ////No use here;
panel4.add(infol);
panel4.add(info);
cp.add(panel1);
cp.add(panel2);
cp.add(panel3);
cp.add(panel4);
cp.add(tabs);
//End of UI
sermode.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
connect.setText(listens);
stop.setEnabled(false);
connect.setEnabled(true);
parent.setTitle("NetBox-Server");
}
});
climode.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
connect.setText(connects);
stop.setEnabled(false);
connect.setEnabled(true);
parent.setTitle("NetBox-Client");
}
});
connect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(climode.isSelected()){ //client mode.
sermode.setEnabled(false);
climode.setEnabled(false);
host.setEnabled(false);
port.setEnabled(false);
connect.setEnabled(false);
Socket socket;
try{
socket =new Socket(host.getText(),(new Integer(port.getText())).intValue());
tabs.add((socket.getInetAddress()).getHostAddress(),CreateTab(tabs,socket));
parent.pack();
}catch(Exception ex){
info.setText(ex.getMessage());
}
sermode.setEnabled(true);
climode.setEnabled(true);
host.setEnabled(true);
port.setEnabled(true);
connect.setEnabled(true);
}
if(sermode.isSelected()){
sermode.setEnabled(false);
climode.setEnabled(false);
host.setEnabled(false);
port.setEnabled(false);
connect.setEnabled(false);
stop.setEnabled(true);
ListenThread=new MyThread(){
ServerSocket server;
public void run(){
connect.setEnabled(false);
stop.setEnabled(true);
try{
server=new ServerSocket((new Integer(port.getText())).intValue());
parent.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
try{
if(server!=null)server.close();
}catch(Exception ex){};
}
});
info.setText("Bind:"+server.toString());
////////////////host.setText((server.getInetAddress()).getHostAddress());
while(true){
Socket socket=server.accept();
tabs.add((socket.getInetAddress()).getHostAddress(),CreateTab(tabs,socket));
parent.pack();
}
}catch(Exception e){
info.setText(e.getMessage());
if(server!=null&&!server.isClosed())close();
}
}
public void close(){
try{
if(server!=null){
info.setText("Closing listen socket");
server.close();
if(!server.isClosed()){throw new Exception("Close fail");}
server=null;
}
}catch(Exception ex){
info.setText(ex.getMessage());
}
ListenThread=null;
sermode.setEnabled(true);
climode.setEnabled(true);
host.setEnabled(true);
port.setEnabled(true);
connect.setEnabled(true);
stop.setEnabled(false);
this.destroy();
}
};
ListenThread.start();
}
}
});
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(ListenThread!=null){
ListenThread.close();
}
stop.setEnabled(false);
connect.setEnabled(true);
}
});
parent.pack();
parent.setVisible(true);
parent.setResizable(false);
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
}
new NetBox();
}
}
[/PHP]
附件里有全部代码。.rar改为.jar 就可以运行。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|