📄 friendjlist.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FriendJList extends JFrame{
private JTextField jTxt;
private JButton jbtn_FriendList;
JList myJList ;
private JButton jbtn_group;
private JButton jbtn_recntTouchFriend;
private Container container ;
private JScrollPane jScrollPane;
private DefaultListModel jListModel;
//ChatUI chatUI;
UDPTalk udpTalk;
Vector users;
public FriendJList(String strTitle,Vector users, final UDPTalk udpTalk){
super(strTitle+":"+((User)users.get(0)).getId());
final int thisUserID = ((User)users.get(0)).getId();
this.users = users;
this.udpTalk = udpTalk;
jTxt = new JTextField(20);
jbtn_FriendList = new JButton("QQ好友");
myJList = new JList();
jbtn_group = new JButton("QQ群");
jbtn_recntTouchFriend = new JButton("最近联系人");
myJList.setCellRenderer(new JLabelListCellRenderer());
jListModel = new DefaultListModel();
//jTxt.setEditable(false);
jScrollPane = new JScrollPane(myJList);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
myJList.setModel(jListModel);
jTxt.setBounds(0,0,200,20);
jbtn_FriendList.setBounds(25,25,165,20);
jScrollPane.setBounds(25,45,165,500);
jbtn_group.setBounds(25,547,165,20);
jbtn_recntTouchFriend.setBounds(25,570,165,20);
container = this.getContentPane();
container.setLayout(null);
container.add(jTxt);
container.add(jbtn_FriendList);
container.add(jScrollPane);
container.add(jbtn_group);
container.add(jbtn_recntTouchFriend);
this.setSize(new Dimension(200,650));
this.setVisible(true);
this.addAll(users);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myJList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
}
});
myJList.addMouseListener(new FriendJListMouseListener(this,udpTalk));
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
/* try {
udpTalk.send("quit#"+thisUserID, "127.0.0.1", 8006);
} catch (IOException e) {
e.printStackTrace();
}*/
}
});
}
public void add(User user){
jListModel.addElement(user);
}
public void addAll(Vector users){
Iterator i =users.iterator();
while(i.hasNext()){
User user = (User)i.next();
P.p("addAll: add usrid="+user.getId());
add(user);
}
}
/*public static void main(String[] args) throws InterruptedException {
Vector users = new Vector();
users.add(new User(1000,new ImageIcon("e.png")));
users.add(new User(1001,new ImageIcon("51a.jpg")));
users.add(new User(1002,new ImageIcon("zidan.jpg")));
FriendJList fj = new FriendJList("QQ",users);
Thread.sleep(5000);
fj.add(new User(1003,new ImageIcon("")));
P.p("result");
}*/
}
class FriendJListMouseListener extends MouseAdapter{
private FriendJList friendJList = null;
private UDPTalk udpTalk = null;
public FriendJListMouseListener (FriendJList fl,UDPTalk udtalk){
friendJList = fl;
this.udpTalk = udpTalk;
}
public void mouseClicked(MouseEvent me){
if(me.getClickCount()==2){
final int index = friendJList.myJList.locationToIndex(me.getPoint());
if(friendJList.users.size()>1){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
P.p("聊天窗口");P.p("Double clicked on Item " + index);
//为什么在此处没有响应事件,为什么下一句的...get(index).getChatUI()=null不为真(关闭ChatUI时,把chatUI赋为空了)
if(((User)friendJList.users.get(index)).getChatUI()==null){
User localUser = (User)friendJList.users.get(0);
User remoteUser = (User)friendJList.users.get(index);
P.p("localUserID="+localUser.id+"remoteUserID="+remoteUser.id);
ChatUI chatUI = new ChatUI(localUser,remoteUser,friendJList.udpTalk);
((User)friendJList.users.get(index)).setChatUI(chatUI);
}
}
});
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -