JAVA入门到精通-第88讲-山寨QQ项目2-好友列表界面

好友列表界面:

卡片整个是一个JPanel,Border布局的Panel
JButton,我的好友;
中间也是一个JPanel
JScrollPane该控件放了一个JPanel(GridLayout(10,1))好友列表;
网格布局,10-10个好友;
每一个好友是一个Label;
最南边又是一个 JPanel,GridLayout(2,1)放两个按钮;
有3张卡片,3个JPanel
----------------------------
(4,4)表示上下行的间隔;行间距、列间距;
-----------------
第三个Panel
2行1列:(陌生人、黑名单)
jphy3=new JPanel(new GridLayout(2,1));
---------------------
JScrollPane放的是jphy2
把第二个Panel放入滚动的JScrollPane里面
---------------------
---------------------
50个好友是:   JLabel []jbls=new JLable[50]
QQ好友编号 (i+1),往左边放JLabel.LEFT
--------------------------
//对jphy1初始化,整个一个大块
最终需要放在 JFrame里面的;this.add(xxx);

===================
分析第二个布局:
处理第二张卡片(陌生人)
我的好友
陌生人:

黑名单

//处理第二张卡片

“我的好友”、“陌生人”两个按钮加进去;


加入jpmsr1
---------------------------------
两个Panel有切换的效果:
把整个JFrame设置成 CardLayout布局

-----------------------
监听“陌生人”按钮

addActionListener

actionPerformed
不允许对JPanel直接控制;
加上 getContentPane( ) 就好;

--------------------
对“我的好友”做个监听
如果等于jmsr_jb1“我的好友”这个按钮,
就显示第一张卡片;
c1.show(this.getContentPane(), "1");

缓慢地动:flash技术开发;
----------------------------
“当鼠标移到人头有高亮的效果”:
(1)对每一个标签做一个监听;
 MouseListener
arg0强转为JLabel
JLabel  j1=(JLabel) arg0.getSource();

-------------------
把红色改回去,鼠标退出-black
mouseExited
----------------------------------
鼠标双击和人聊天
响应用户双击的事件,并得到好友的编号:
getClickCount () == 2
强转为JLabel,因为要从 JLabel中取数据;
------------------------
双击某一个人,跳出一个窗口,和xxx聊天

258
 
1
/**
2
 * 功能:我的好友列表界面(也包括陌生人、黑名单)
3
 */
4
package com.qq.client.view;
5
 
6
import java.awt.BorderLayout;
7
import java.awt.CardLayout;
8
import java.awt.Color;
9
import java.awt.GridLayout;
10
import java.awt.event.ActionEvent;
11
import java.awt.event.ActionListener;
12
import java.awt.event.MouseEvent;
13
import java.awt.event.MouseListener;
14
import javax.swing.ImageIcon;
15
import javax.swing.JButton;
16
import javax.swing.JFrame;
17
import javax.swing.JLabel;
18
import javax.swing.JPanel;
19
import javax.swing.JScrollPane;
20
import com.qq.client.tools.ManageQqChat;
21
import com.qq.common.Message;
22
 
23
public class QqFriendList extends JFrame implements ActionListener,MouseListener{
24
    //处理第一张卡片(我的好友)
25
    JPanel jphy1,jphy2,jphy3;
26
    JButton jphy1_jb1,jphy1_jb2,jphy1_jb3;
27
    JScrollPane jsp1;
28
    String ownerId;
29
    JLabel []jbls;
30
   
31
    //处理第二张卡片(陌生人)
32
    JPanel jpmsr1,jpmsr2,jpmsr3;
33
    JButton jpmsr_jb1,jpmsr_jb2,jpmsr_jb3;
34
    JScrollPane jsp2;
35
   
36
    //处理第三张卡片(黑名单)
37
    JPanel jphmd1,jphmd2,jphmd3;
38
    JButton jphmd_jb1,jphmd_jb2,jphmd_jb3;
39
    JScrollPane jsp3;
40
   
41
    //把整个JFrame设置成CardLayout
42
    CardLayout cl;
43
   
44
//  public static void main(String[] args) {
45
//      new QqFriendList();
46
//  }
47
   
48
    public void initCard1(){
49
        //处理第一张卡片(显示好友列表)
50
        jphy1_jb1=new JButton("我的好友");
51
        jphy1_jb2=new JButton("陌生人");
52
        jphy1_jb2.addActionListener(this);
53
        jphy1_jb3=new JButton("黑名单");
54
        jphy1_jb3.addActionListener(this);
55
        jphy1=new JPanel(new BorderLayout());
56
        //假定有50个好友
57
        jphy2=new JPanel(new GridLayout(50, 1, 4, 4));
58
        //给jp_hy2初始化50个好友
59
        jbls=new JLabel[50];
60
        for(int i=0;i<jbls.length;i++){
61
            jbls[i]=new JLabel(i+1+"",new ImageIcon("image/tou1_1.jpg"),JLabel.LEFT);
62
            jbls[i].setEnabled(false);
63
            //判断在线
64
            if(jbls[i].getText().equals(ownerId)){
65
                jbls[i].setEnabled(true);
66
            }
67
            jbls[i].addMouseListener(this);
68
            jphy2.add(jbls[i]);
69
        }
70
        jphy3=new JPanel(new GridLayout(2, 1));
71
        //把两个按钮加入到jp_hy3中
72
        jphy3.add(jphy1_jb2);
73
        jphy3.add(jphy1_jb3);
74
 
75
        jsp1=new JScrollPane(jphy2);
76
        //对jp_hy1初始化
77
        jphy1.add(jphy1_jb1,"North");
78
        jphy1.add(jsp1,"Center");
79
        jphy1.add(jphy3,"South");
80
    }
81
   
82
    public void initCard2(){
83
        //处理第二张卡片
84
        jpmsr_jb1=new JButton("我的好友");
85
        jpmsr_jb1.addActionListener(this);
86
        jpmsr_jb2=new JButton("陌生人");
87
        jpmsr_jb3=new JButton("黑名单");
88
        jpmsr_jb3.addActionListener(this);
89
        jpmsr1=new JPanel(new BorderLayout());
90
        //假定有20个陌生人
91
        jpmsr2=new JPanel(new GridLayout(20, 1, 4, 4));
92
        //给jp_hy2初始化20个陌生人
93
        JLabel []jbls2=new JLabel[20];
94
        for(int i=0;i<jbls2.length;i++){
95
            jbls2[i]=new JLabel(i+1+"",new ImageIcon("image/tou1_1.jpg"),JLabel.LEFT);
96
            jbls2[i].setEnabled(false);
97
            if(jbls2[i].getText().equals(ownerId)){
98
                jbls2[i].setEnabled(true);
99
            }
100
            jbls2[i].addMouseListener(this);
101
            jpmsr2.add(jbls2[i]);
102
        }
103
        jpmsr3=new JPanel(new GridLayout(2, 1));
104
        //把两个按钮加入到jp_hy3中
105
        jpmsr3.add(jpmsr_jb1);
106
        jpmsr3.add(jpmsr_jb2);
107
 
108
        jsp2=new JScrollPane(jpmsr2);
109
        //对jp_hy1初始化
110
        jpmsr1.add(jpmsr3,"North");
111
        jpmsr1.add(jsp2,"Center");
112
        jpmsr1.add(jpmsr_jb3,"South");
113
    }
114
   
115
    public void initCard3(){
116
        //处理第三张卡片(黑名单)
117
        jphmd_jb1=new JButton("我的好友");
118
        jphmd_jb1.addActionListener(this);
119
        jphmd_jb2=new JButton("陌生人");
120
        jphmd_jb2.addActionListener(this);
121
        jphmd_jb3=new JButton("黑名单");
122
 
123
        jphmd1=new JPanel(new BorderLayout());
124
        //假定有5个黑名单
125
        jphmd2=new JPanel(new GridLayout(5, 1, 4, 4));
126
        //给jp_hy2初始化5个黑名单
127
        JLabel []jbls3=new JLabel[5];
128
        for(int i=0;i<jbls3.length;i++){
129
            jbls3[i]=new JLabel(i+1+"",new ImageIcon("image/tou1_1.jpg"),JLabel.LEFT);
130
            jbls3[i].setEnabled(false);
131
            if(jbls3[i].getText().equals(ownerId)){
132
                jbls3[i].setEnabled(true);
133
            }
134
            jbls3[i].addMouseListener(this);
135
            jphmd2.add(jbls3[i]);
136
        }
137
        jphmd3=new JPanel(new GridLayout(3, 1));
138
        //把两个按钮加入到jp_hy3中
139
        jphmd3.add(jphmd_jb1);
140
        jphmd3.add(jphmd_jb2);
141
        jphmd3.add(jphmd_jb3);
142
 
143
        jsp3=new JScrollPane(jphmd2);
144
        //对jp_hy1初始化
145
        jphmd1.add(jphmd3,"North");
146
        jphmd1.add(jsp3,"Center");
147
    }
148
   
149
    //更新在线的好友情况
150
    public void updateFriend(Message m){
151
        String onLineFriend[]=m.getCon().split(" ");
152
        for(int i=0;i<onLineFriend.length;i++){
153
            jbls[Integer.parseInt(onLineFriend[i])-1].setEnabled(true);
154
        }
155
    }
156
   
157
    //构造函数
158
    public QqFriendList(String ownerId){
159
        this.ownerId=ownerId;
160
        this.initCard1();
161
        this.initCard2();
162
        this.initCard3();
163
 
164
        cl=new CardLayout();
165
        this.setLayout(cl);
166
        this.add(jphy1,"1");
167
        this.add(jpmsr1,"2");
168
        this.add(jphmd1,"3");
169
        //在窗口显示自己的编号
170
        this.setTitle(ownerId);
171
        this.setSize(150, 450);
172
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
173
        this.setVisible(true);
174
    }
175
 
176
    @Override
177
    public void actionPerformed(ActionEvent e) {
178
        //如果点击了我的好友中的陌生人按钮,就显示第2张卡片
179
        if(e.getSource()==jphy1_jb2){
180
            cl.show(this.getContentPane(),"2");
181
        }
182
        //如果点击了我的好友中的黑名单按钮,就显示第3张卡片
183
        else if(e.getSource()==jphy1_jb3){
184
            cl.show(this.getContentPane(),"3");
185
        }
186
        //如果点击了陌生人中的我的好友按钮,就显示第1张卡片
187
        else if(e.getSource()==jpmsr_jb1){
188
            cl.show(this.getContentPane(), "1");
189
        }
190
        //如果点击了陌生人中的黑名单按钮,就显示第3张卡片
191
        else if(e.getSource()==jpmsr_jb3){
192
            cl.show(this.getContentPane(),"3");
193
        }
194
        //如果点击了黑名单中的我的好友按钮,就显示第1张卡片
195
        else if(e.getSource()==jphmd_jb1){
196
            cl.show(this.getContentPane(),"1");
197
        }
198
        //如果点击了黑名单中的陌生人按钮,就显示第2张卡片
199
        else if(e.getSource()==jphmd_jb2){
200
            cl.show(this.getContentPane(),"2");
201
        }
202
    }
203
 
204
    @Override
205
    public void mouseClicked(MouseEvent e) {
206
        //响应用户双击鼠标左键的事件,并得到好友的编号
207
        if(e.getButton()==1&&e.getClickCount()==2){
208
            //得到该好友的编号
209
            String friendNo=((JLabel)e.getSource()).getText();
210
            QqChat qqChat=new QqChat(friendNo,this.ownerId);
211
           
212
            //把聊天界面加入到管理类中
213
            ManageQqChat.addQqChat(this.ownerId+" "+friendNo, qqChat);
214
        }
215
    }
216
 
217
    @Override
218
    public void mousePressed(MouseEvent e) {
219
        // TODO Auto-generated method stub
220
    }
221
 
222
    @Override
223
    public void mouseReleased(MouseEvent e) {
224
        // TODO Auto-generated method stub
225
    }
226
 
227
    @Override
228
    public void mouseEntered(MouseEvent e) {
229
        JLabel jl=(JLabel)e.getSource();
230
        jl.setForeground(Color.red);
231
    }
232
 
233
    @Override
234
    public void mouseExited(MouseEvent e) {
235
        JLabel jl=(JLabel)e.getSource();
236
        jl.setForeground(Color.black);
237
    }
238
}
239





猜你喜欢

转载自www.cnblogs.com/xuxaut-558/p/10047899.html
今日推荐