📄 mainwindow.java
字号:
/*作者:吴拓 Email:setsail_wu@163.com
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.StringTokenizer;
import javax.swing.JSplitPane;
public class MainWindow extends Frame implements Runnable{
/*========== Constant Value ==========*/
public static final long IPMSG_COMMASK = 0x000000ffL;
public static final long IPMSG_OPTMASK = 0xffffff00L;
public static final long IPMSG_NOOPERATION = 0x00000000L;
public static final long IPMSG_BR_ENTRY = 0x00000001L;
public static final long IPMSG_BR_EXIT = 0x00000002L;
public static final long IPMSG_ANSENTRY = 0x00000003L;
public static final long IPMSG_BR_ABSENCE = 0x00000004L;
public static final long IPMSG_BR_ISGETLIST = 0x00000018L;
public static final long IPMSG_OKGETLIST = 0x00000015L;
public static final long IPMSG_GETLIST = 0x00000016L;
public static final long IPMSG_ANSLIST = 0x00000017L;
public static final long IPMSG_SENDMSG = 0x00000020L;
public static final long IPMSG_RECVMSG = 0x00000021L;
public static final long IPMSG_READMSG = 0x00000030L;
public static final long IPMSG_DELMSG = 0x00000031L;
public static final long IPMSG_GETINFO = 0x00000040L;
public static final long IPMSG_SENDINFO = 0x00000041L;
public static final long IPMSG_GETPUBKEY = 0x00000072L;
public static final long IPMSG_ANSPUBKEY = 0x00000073L;
// other opt
public static final long IPMSG_ABSENCEOPT = 0x00000100L;
public static final long IPMSG_SERVEROPT = 0x00000200L;
public static final long IPMSG_DIALUPOPT = 0x00010000L;
public static final long IPMSG_ENCRYPTOPT = 0x00400000L;
public static final long IPMSG_ENCRYPTOPTOLD = 0x00800000L;
// send opt
public static final long IPMSG_SENDCHECKOPT = 0x00000100L;
public static final long IPMSG_SECRETOPT = 0x00000200L;
public static final long IPMSG_BROADCASTOPT = 0x00000400L;
public static final long IPMSG_MULTICASTOPT = 0x00000800L;
public static final long IPMSG_NOPOPUPOPT = 0x00001000L;
public static final long IPMSG_AUTORETOPT = 0x00002000L;
public static final long IPMSG_RETRYOPT = 0x00004000L;
public static final long IPMSG_PASSWORDOPT = 0x00008000L;
public static final long IPMSG_NOLOGOPT = 0x00020000L;
public static final long IPMSG_NEWMUTIOPT = 0x00040000L;
// encrypt opt
public static final long IPMSG_RSA_512 = 0x00000001L;
public static final long IPMSG_RSA_1024 = 0x00000002L;
public static final long IPMSG_RSA_2048 = 0x00000004L;
public static final long IPMSG_RC2_40 = 0x00001000L;
public static final long IPMSG_RC2_128 = 0x00004000L;
public static final long IPMSG_RC2_256 = 0x00008000L;
public static final long IPMSG_BLOWFISH_128 = 0x00020000L;
public static final long IPMSG_BLOWFISH_256 = 0x00040000L;
public static final int MAXBUF = 1024;
/*========== end ==========*/
public List memberlist;
public Button refresh, send;
public Label inlineNumberLabel;
public TextArea editTextArea;
private DatagramSocket UdpMsgds = null;
public Thread refreshList = null;
public Hashtable hashtable = new Hashtable();
public MessengePack msgpack;
public UdpMsgRecv udpmsgrecvThread;
public int inlinenumber = 0;
public InetAddress hostip;
public MainWindow() {
createWindow();
//定时接收信息
try{
UdpMsgds = new DatagramSocket(null);
UdpMsgds.bind(new InetSocketAddress(2425));
}catch(SocketException e){
System.out.println("bind error" + e);
}
udpmsgrecvThread = new UdpMsgRecv(UdpMsgds);
udpmsgrecvThread.start();
//定时刷新局域网内用户列表
refreshList = new Thread(this);
refreshList.start();
}
public void createWindow(){
setTitle("JavaIPM 0.1");
setLayout(new BorderLayout());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
udpmsgrecvThread.broadcast(0x00000002L);//离线广播。
System.exit(0);
}
});
Panel p1 = new Panel(new BorderLayout());
memberlist = new List(8, true);
memberlist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//sendAction();
}
});
p1.add("Center", memberlist);
p1.add("North", new Label("用户名 主机名 IP地址"));
Panel p2 = new Panel(new GridLayout(6,0));
p2.add(new Label());
p2.add(new Label());
p2.add(new Label(" 在线用户 "));
inlineNumberLabel = new Label(" 0");
p2.add(inlineNumberLabel);
refresh = new Button();
refresh.setLabel("刷新");
refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
memberlist.removeAll();
udpmsgrecvThread.hashtable.clear();
inlinenumber = 0;
udpmsgrecvThread.broadcast(0x00000001L);//上线广播。
refreshAction();
}
});
p2.add(refresh);
//p2.setSize(100,50);
p1.add("East", p2);
editTextArea = new TextArea("", 5, 40);
Panel p3 = new Panel(new GridLayout(0,5));
p3.add(new Label());
send = new Button();
send.setLabel("发送");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sendAction();
}
});
p3.add(send);
Panel Psouth = new Panel (new BorderLayout());
Psouth.add("Center", editTextArea);
Psouth.add("South", p3);
JSplitPane split;
split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p1, Psouth);
add(split,BorderLayout.CENTER);
setLocation(200, 200);
setSize(350, 300);
setIconImage(getToolkit().getImage(
getClass().getResource("images/ipmsg.gif")));
}
public void sendAction() {
String slectmember[] = memberlist.getSelectedItems();
String ipString;
System.out.println(slectmember.length);
for( int i = 0; i < slectmember.length; i++){
StringTokenizer tokenizer = new StringTokenizer(slectmember[i], "/", false);
tokenizer.nextToken();
ipString = tokenizer.nextToken();
try{
hostip = hostip.getByName(ipString);
}catch(Exception e){
System.out.println("MainWindows.java:sendAction:error:" + e );
}
udpmsgrecvThread.mysend(IPMSG_SENDMSG, editTextArea.getText(), hostip);
}
}
public void refreshAction() {
memberlist.removeAll();
hashtable = udpmsgrecvThread.gethashtable();
inlineNumberLabel.setText(" " + hashtable.size());
for (Enumeration e = hashtable.elements() ; e.hasMoreElements() ;) {
msgpack = (MessengePack)e.nextElement();
memberlist.add(msgpack.getUser() + " " + msgpack.getHost()
+ " " +msgpack.gethostip());
}
}
public void run(){
hashtable = udpmsgrecvThread.gethashtable();
while(true){
if(inlinenumber != hashtable.size()){
this.refreshAction();
inlinenumber = hashtable.size();
}
try{
refreshList.sleep(1000);
}catch(InterruptedException e){
System.out.println("MainWindow.java:refreshList.sleep(500):error:" + e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -