📄 mainwin.java
字号:
int index; //get list index
int index3; //get firiend onlineinfo
int index4; //get message from info
boolean fromunknow = false;
//以下建立窗口类
FindFriend2 findf;
JDialog hello = new JDialog();
JDialog OneAddyou = new JDialog();
JDialog DirectAdd = new JDialog();
int tempgetjicq; //get the tempgetjicq
/////////////////////////////friend info
//以下是程序界面的变量
ImageIcon icon1 = new ImageIcon("cab_small.gif");
ImageIcon icon6 = new ImageIcon("sun_small.gif");
JButton jButton1 = new JButton();
JButton direct = new JButton();
JLabel info = new JLabel();
JDialog about = new JDialog();
JDialog senddata = new JDialog();
JDialog getdata = new JDialog();
JButton ok = new JButton();
JPopupMenu jPopupMenu1 = new JPopupMenu();
JMenuItem sendmessage = new JMenuItem();
JMenuItem getmessage = new JMenuItem();
JMenuItem lookinfo = new JMenuItem();
JMenuItem chatrecord = new JMenuItem();
JLabel name = new JLabel();
JTextField nametext = new JTextField();
JLabel icq = new JLabel();
JTextField icqno = new JTextField();
JButton send = new JButton();
JButton cancel = new JButton();
JTextArea sendtext = new JTextArea();
JList list;
JLabel jLabel2 = new JLabel();
JButton find = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
//*************net
//以下是网络相关变量
Socket socket;
BufferedReader in;
PrintWriter out;
DatagramPacket sendPacket, receivePacket;
DatagramSocket sendSocket, receiveSocket;
int udpPORT = 5001; //
int sendPort = 5000; //单机调试请改动这里,谢谢!!!!!
String server;
int serverport;
byte array[] = new byte[512];
Thread thread;
int myjicq;
String received;
////以下是程序界面的变量
JLabel jLabel3 = new JLabel();
JTextField getfromname = new JTextField();
JLabel jLabel4 = new JLabel();
JTextField getfromjicq = new JTextField();
JTextArea getinfo = new JTextArea();
JButton getok = new JButton();
String theip;
JButton update = new JButton();
JMenuItem delfriend = new JMenuItem();
JButton myinfo = new JButton();
JButton online = new JButton();
JLabel jLabel1 = new JLabel();
JTextField helloname = new JTextField();
JLabel jLabel5 = new JLabel();
JTextField hellojicq = new JTextField();
JLabel jLabel6 = new JLabel();
JTextField helloemail = new JTextField();
JLabel jLabel7 = new JLabel();
JTextArea helloinfo = new JTextArea();
JButton jButton3 = new JButton();
JButton hellook = new JButton();
JLabel jLabel8 = new JLabel();
JLabel jLabel9 = new JLabel();
JLabel jLabel10 = new JLabel();
JLabel oneaddme = new JLabel();
JButton addit = new JButton();
JButton iknow = new JButton();
JLabel jLabel11 = new JLabel();
JLabel jLabel12 = new JLabel();
JTextField friendid = new JTextField();
JButton directaddok = new JButton();
//***************net
//以下该函数连接服务器
public void ConnectServer(int myid) {
try {
socket = new Socket(InetAddress.getByName(server), serverport);
in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
//this is call my friend info
//以下读取好友信息
out.println("friend");
out.println(myid);
friendnum = Integer.parseInt(in.readLine());
String friendname = " ";
String friendjicqno, friendip, friendstatus, picinfo, email, infos;
do {
friendname = in.readLine();
if (friendname.equals("over"))
break;
friendnames.add(friendname);
friendjicqno = in.readLine();
friendjicq.add(new Integer(friendjicqno));
friendip = in.readLine();
friendips.add(friendip);
friendstatus = in.readLine();
status.add(friendstatus);
picinfo = in.readLine();
picno.add(new Integer(picinfo));
email = in.readLine();
friendemail.add(email);
infos = in.readLine();
friendinfo.add(infos);
} while (!friendname.equals("over"));
} catch (IOException e1) {
System.out.println("false");
}
//以下在列表中显示好友
DefaultListModel mm = (DefaultListModel) list.getModel();
int picid;
for (int p = 0; p < friendnames.size(); p++) {
picid = Integer.parseInt(picno.get(p).toString());
if (status.get(p).equals("1")) {
mm.addElement(
new Object[] {
friendnames.get(p),
new ImageIcon(picsonline[picid])});
} else {
mm.addElement(
new Object[] {
friendnames.get(p),
new ImageIcon(picsoffline[picid])});
}
} //for
} //connectto server
//*****************************
//以下函数无限监听好友的消息
public void run() {
while (true) {
try {
for (int x = 0; x < 512; x++)
array[x] = ' ';
//创建数据报
receivePacket = new DatagramPacket(array, array.length);
receiveSocket.receive(receivePacket);
byte[] data = receivePacket.getData();
String infofromip =
receivePacket
.getAddress()
.getHostAddress()
.toString()
.trim();
index3 = 0;
received = new String(data, 0, data.length);
received.trim();
// System.out.println("get"+received.substring(0,6));
String tempstr;
int tx;
//friend online
if (received.substring(0, 6).equals("online")) { //如果有好友上线就变彩色
tempstr = received.substring(6).trim();
// System.out.println("str"+tempstr);
tempgetjicq = Integer.parseInt(tempstr);
// System.out.println("id"+tempgetjicq);
do {
tx =
Integer.parseInt(friendjicq.get(index3).toString());
//System.out.println("tx"+tx);
if (tempgetjicq == tx)
break;
index3++;
} while (index3 < friendjicq.size());
friendips.setElementAt(infofromip, index3);
// status.setElementAt(,index3);
//System.out.println(index3);
DefaultListModel mm3 = (DefaultListModel) list.getModel();
int picid = Integer.parseInt(picno.get(index3).toString());
mm3.setElementAt(
new Object[] {
friendnames.get(index3),
new ImageIcon(picsonline[picid])},
index3);
} //end online
//friend offline
else if (
received.substring(0, 7).equals("offline")) { //如果有好友下线就变灰色
tempstr = received.substring(7).trim();
System.out.println("str" + tempstr);
tempgetjicq = Integer.parseInt(tempstr);
System.out.println("id" + tempgetjicq);
do {
tx =
Integer.parseInt(friendjicq.get(index3).toString());
System.out.println("tx" + tx);
if (tempgetjicq == tx)
break;
index3++;
} while (index3 < friendjicq.size());
infofromip = "null";
friendips.setElementAt(infofromip, index3);
// status.setElementAt(,index3);
System.out.println(index3);
DefaultListModel mm3 = (DefaultListModel) list.getModel();
int picid = Integer.parseInt(picno.get(index3).toString());
mm3.setElementAt(
new Object[] {
friendnames.get(index3),
new ImageIcon(picsoffline[picid])},
index3);
} //end friend offline
//someone add me as friend
else if (received.substring(0, 9).equals("oneaddyou")) {
//如果有人加我为好有,选择加还是不
tempstr = received.substring(9).trim();
System.out.println("str" + tempstr);
tempgetjicq = Integer.parseInt(tempstr);
System.out.println("id" + tempgetjicq);
//JOptionPane.showMessageDialog(this,"收到"+tempgetjicq+"addyou","ok",JOptionPane.INFORMATION_MESSAGE);
oneaddme.setText(tempgetjicq + "把你加为好友");
OneAddyou.setBounds(400, 300, 250, 200);
OneAddyou.show();
} //endsomeone add me as friend
else { //否则就显示收到消息
index4 = 0;
// String infofromip=receivePacket.getAddress().getHostAddress().toString().trim();
do {
String friendip =
friendips.get(index4).toString().trim();
if (infofromip.equals(friendip)) {
String nameinfo =
friendnames.get(index4).toString().trim();
JOptionPane.showMessageDialog(
this,
"收到" + nameinfo + "的消息",
"ok",
JOptionPane.INFORMATION_MESSAGE);
fromunknow = false;
break;
} //if
index4++;
if (index4 >= friendnames.size()) {
fromunknow = true; //收到陌生人的消息
JOptionPane.showMessageDialog(
this,
"收到陌生人" + infofromip + "的消息",
"ok",
JOptionPane.INFORMATION_MESSAGE);
}
} while (index4 < friendnames.size()); //while
System.out.println(index4);
};
} catch (IOException ex) {
ex.printStackTrace();
}
}
} //run end
//**********************
//以下创建数据报
public void CreatUDP() {
try {
sendSocket = new DatagramSocket();
receiveSocket = new DatagramSocket(udpPORT);
// System.out.println("udp ok");
} catch (SocketException se) {
se.printStackTrace();
System.out.println("false udp");
}
} // creat udp end
//main ****************
public MainWin(int s, String sername, int serport) { //主程序构造函数
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
myjicq = s;
server = sername;
serverport = serport;
jbInit();
ConnectServer(myjicq);
CreatUDP();
findf = new FindFriend2(myjicq, server, serverport);
findf.setBounds(200, 150, 300, 300);
thread = new Thread(this);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
} //end main*****
/**Component initialization*/
private void jbInit() throws Exception { //主程序界面
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(flowLayout1);
this.getContentPane().setBackground(new Color(132, 158, 203));
this.setResizable(false);
this.setSize(new Dimension(206, 420));
this.setTitle("Frame Title");
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
this_mousePressed(e);
}
});
ListModel model = new NameAndPicListModel(friendnames, picsonline);
ListCellRenderer renderer = new NameAndPicListCellRenderer();
list = new JList(model);
list.setBackground(new Color(255, 255, 210));
list.setAlignmentX((float) 1.0);
list.setAlignmentY((float) 1.0);
list.setCellRenderer(renderer);
list.setVisibleRowCount(7);
list.addMouseListener(new MainWin_list_mouseAdapter(this));
list.setSize(380, 200);
jButton1.setText("我的好友");
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);
jButton1.setIcon(icon1);
jButton1.setPressedIcon(icon6);
direct.setText("直接加友");
direct.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
direct_mouseClicked(e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -