博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA入门到精通-第88讲-山寨QQ项目2-好友列表界面
阅读量:7164 次
发布时间:2019-06-29

本文共 14530 字,大约阅读时间需要 48 分钟。

好友列表界面:
1167156-20181201003520592-2114587765.png
卡片整个是一个JPanel,Border布局的Panel
JButton,我的好友;
中间也是一个JPanel
JScrollPane该控件放了一个JPanel(GridLayout(10,1))好友列表;
网格布局,10-10个好友;
每一个好友是一个Label;
最南边又是一个
JPanel,GridLayout(2,1)放两个按钮;
有3张卡片,3个JPanel
1167156-20181201003521287-1607563193.png
----------------------------
1167156-20181201003521475-1515858226.png
(4,4)表示上下行的间隔;行间距、列间距;
-----------------
第三个Panel
2行1列:(陌生人、黑名单)
jphy3=new JPanel(new GridLayout(2,1));
1167156-20181201003521710-1933380535.png
---------------------
JScrollPane放的是jphy2
1167156-20181201003521896-678596163.png
把第二个Panel放入滚动的JScrollPane里面
---------------------
1167156-20181201003522064-1304620200.png
---------------------
50个好友是:   JLabel []jbls=new JLable[50]
1167156-20181201003522292-1826396510.png
QQ好友编号
(i+1),往左边放JLabel.LEFT
--------------------------
//对jphy1初始化,整个一个大块
最终需要放在
JFrame里面的;this.add(xxx);
1167156-20181201003523275-1656053428.png
1167156-20181201003523621-1991556915.png
===================
分析第二个布局:
处理第二张卡片(陌生人)
我的好友
陌生人:
黑名单
1167156-20181201003523912-2055908089.png
//处理第二张卡片
“我的好友”、“陌生人”两个按钮加进去;
1167156-20181201003524503-526097584.png
1167156-20181201003524727-908015809.png
加入jpmsr1
1167156-20181201003525317-1496664360.png
---------------------------------
两个Panel有切换的效果:
把整个JFrame设置成
CardLayout布局
1167156-20181201003525681-1747526207.png
-----------------------
监听“陌生人”按钮
1167156-20181201003525917-80000873.png
addActionListener
1167156-20181201003526848-495967927.png
actionPerformed
1167156-20181201003527086-1959104266.png
不允许对JPanel直接控制;
加上
getContentPane( ) 就好;
1167156-20181201003527485-162127661.png
--------------------
对“我的好友”做个监听
1167156-20181201003527964-1583948045.png
1167156-20181201003528536-844101683.png
如果等于jmsr_jb1“我的好友”这个按钮,
就显示第一张卡片;
c1.show(this.getContentPane(), "1");
1167156-20181201003528845-2039931095.png
缓慢地动:flash技术开发;
----------------------------
“当鼠标移到人头有高亮的效果”:
(1)对每一个标签做一个监听;
 MouseListener
1167156-20181201003529068-828079061.png
arg0强转为JLabel
JLabel  j1=(JLabel) arg0.getSource();
1167156-20181201003529596-1947444218.png
1167156-20181201003530295-159520958.png
-------------------
把红色改回去,鼠标退出-black
mouseExited
1167156-20181201003531002-581337641.png
----------------------------------
鼠标双击和人聊天
响应用户双击的事件,并得到好友的编号:
getClickCount () == 2
强转为JLabel,因为要从 JLabel中取数据;
1167156-20181201003531350-213094042.png
1167156-20181201003531826-423724211.png
------------------------
双击某一个人,跳出一个窗口,和xxx聊天
/** * 功能:我的好友列表界面(也包括陌生人、黑名单) */ package com.qq.client.view; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import com.qq.client.tools.ManageQqChat; import com.qq.common.Message; public class QqFriendList extends JFrame implements ActionListener,MouseListener{ //处理第一张卡片(我的好友) JPanel jphy1,jphy2,jphy3; JButton jphy1_jb1,jphy1_jb2,jphy1_jb3; JScrollPane jsp1; String ownerId; JLabel []jbls; //处理第二张卡片(陌生人) JPanel jpmsr1,jpmsr2,jpmsr3; JButton jpmsr_jb1,jpmsr_jb2,jpmsr_jb3; JScrollPane jsp2; //处理第三张卡片(黑名单) JPanel jphmd1,jphmd2,jphmd3; JButton jphmd_jb1,jphmd_jb2,jphmd_jb3; JScrollPane jsp3; //把整个JFrame设置成CardLayout CardLayout cl; // public static void main(String[] args) { // new QqFriendList(); // } public void initCard1(){ //处理第一张卡片(显示好友列表) jphy1_jb1=new JButton("我的好友"); jphy1_jb2=new JButton("陌生人"); jphy1_jb2.addActionListener(this); jphy1_jb3=new JButton("黑名单"); jphy1_jb3.addActionListener(this); jphy1=new JPanel(new BorderLayout()); //假定有50个好友 jphy2=new JPanel(new GridLayout(50, 1, 4, 4)); //给jp_hy2初始化50个好友 jbls=new JLabel[50]; for(int i=0;i<jbls.length;i++){ jbls[i]=new JLabel(i+1+"",new ImageIcon("image/tou1_1.jpg"),JLabel.LEFT); jbls[i].setEnabled(false); //判断在线 if(jbls[i].getText().equals(ownerId)){ jbls[i].setEnabled(true); } jbls[i].addMouseListener(this); jphy2.add(jbls[i]); } jphy3=new JPanel(new GridLayout(2, 1)); //把两个按钮加入到jp_hy3中 jphy3.add(jphy1_jb2); jphy3.add(jphy1_jb3); jsp1=new JScrollPane(jphy2); //对jp_hy1初始化 jphy1.add(jphy1_jb1,"North"); jphy1.add(jsp1,"Center"); jphy1.add(jphy3,"South"); } public void initCard2(){ //处理第二张卡片 jpmsr_jb1=new JButton("我的好友"); jpmsr_jb1.addActionListener(this); jpmsr_jb2=new JButton("陌生人"); jpmsr_jb3=new JButton("黑名单"); jpmsr_jb3.addActionListener(this); jpmsr1=new JPanel(new BorderLayout()); //假定有20个陌生人 jpmsr2=new JPanel(new GridLayout(20, 1, 4, 4)); //给jp_hy2初始化20个陌生人 JLabel []jbls2=new JLabel[20]; for(int i=0;i<jbls2.length;i++){ jbls2[i]=new JLabel(i+1+"",new ImageIcon("image/tou1_1.jpg"),JLabel.LEFT); jbls2[i].setEnabled(false); if(jbls2[i].getText().equals(ownerId)){ jbls2[i].setEnabled(true); } jbls2[i].addMouseListener(this); jpmsr2.add(jbls2[i]); } jpmsr3=new JPanel(new GridLayout(2, 1)); //把两个按钮加入到jp_hy3中 jpmsr3.add(jpmsr_jb1); jpmsr3.add(jpmsr_jb2); jsp2=new JScrollPane(jpmsr2); //对jp_hy1初始化 jpmsr1.add(jpmsr3,"North"); jpmsr1.add(jsp2,"Center"); jpmsr1.add(jpmsr_jb3,"South"); } public void initCard3(){ //处理第三张卡片(黑名单) jphmd_jb1=new JButton("我的好友"); jphmd_jb1.addActionListener(this); jphmd_jb2=new JButton("陌生人"); jphmd_jb2.addActionListener(this); jphmd_jb3=new JButton("黑名单"); jphmd1=new JPanel(new BorderLayout()); //假定有5个黑名单 jphmd2=new JPanel(new GridLayout(5, 1, 4, 4)); //给jp_hy2初始化5个黑名单 JLabel []jbls3=new JLabel[5]; for(int i=0;i<jbls3.length;i++){ jbls3[i]=new JLabel(i+1+"",new ImageIcon("image/tou1_1.jpg"),JLabel.LEFT); jbls3[i].setEnabled(false); if(jbls3[i].getText().equals(ownerId)){ jbls3[i].setEnabled(true); } jbls3[i].addMouseListener(this); jphmd2.add(jbls3[i]); } jphmd3=new JPanel(new GridLayout(3, 1)); //把两个按钮加入到jp_hy3中 jphmd3.add(jphmd_jb1); jphmd3.add(jphmd_jb2); jphmd3.add(jphmd_jb3); jsp3=new JScrollPane(jphmd2); //对jp_hy1初始化 jphmd1.add(jphmd3,"North"); jphmd1.add(jsp3,"Center"); } //更新在线的好友情况 public void updateFriend(Message m){ String onLineFriend[]=m.getCon().split(" "); for(int i=0;i<onLineFriend.length;i++){ jbls[Integer.parseInt(onLineFriend[i])-1].setEnabled(true); } } //构造函数 public QqFriendList(String ownerId){ this.ownerId=ownerId; this.initCard1(); this.initCard2(); this.initCard3(); cl=new CardLayout(); this.setLayout(cl); this.add(jphy1,"1"); this.add(jpmsr1,"2"); this.add(jphmd1,"3"); //在窗口显示自己的编号 this.setTitle(ownerId); this.setSize(150, 450); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { //如果点击了我的好友中的陌生人按钮,就显示第2张卡片 if(e.getSource()==jphy1_jb2){ cl.show(this.getContentPane(),"2"); } //如果点击了我的好友中的黑名单按钮,就显示第3张卡片 else if(e.getSource()==jphy1_jb3){ cl.show(this.getContentPane(),"3"); } //如果点击了陌生人中的我的好友按钮,就显示第1张卡片 else if(e.getSource()==jpmsr_jb1){ cl.show(this.getContentPane(), "1"); } //如果点击了陌生人中的黑名单按钮,就显示第3张卡片 else if(e.getSource()==jpmsr_jb3){ cl.show(this.getContentPane(),"3"); } //如果点击了黑名单中的我的好友按钮,就显示第1张卡片 else if(e.getSource()==jphmd_jb1){ cl.show(this.getContentPane(),"1"); } //如果点击了黑名单中的陌生人按钮,就显示第2张卡片 else if(e.getSource()==jphmd_jb2){ cl.show(this.getContentPane(),"2"); } } @Override public void mouseClicked(MouseEvent e) { //响应用户双击鼠标左键的事件,并得到好友的编号 if(e.getButton()==1&&e.getClickCount()==2){ //得到该好友的编号 String friendNo=((JLabel)e.getSource()).getText(); QqChat qqChat=new QqChat(friendNo,this.ownerId); //把聊天界面加入到管理类中 ManageQqChat.addQqChat(this.ownerId+" "+friendNo, qqChat); } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { JLabel jl=(JLabel)e.getSource(); jl.setForeground(Color.red); } @Override public void mouseExited(MouseEvent e) { JLabel jl=(JLabel)e.getSource(); jl.setForeground(Color.black); } }
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
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
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
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
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
 

转载于:https://www.cnblogs.com/xuxaut-558/p/10047899.html

你可能感兴趣的文章