QQ好友列表树形列表java代码实现代码

网友投稿 288 2023-07-08


QQ好友列表树形列表java代码实现代码

以前在网上瞎转悠的时候就发现很多人为用java实现QQ登陆后的面板的问题感到十分头疼,最近我因在写模拟QQ的项目,故不可或缺的遇到了这一个问题,在网上我google了,百度了,最终发现的是有很多人被这一问题困扰,却没有解决的方案,估计是那些写出来了的人,没有发布到网上来,如今,经过自己的多方面查找资料,终于把他写出来了,也不枉昨晚熬夜了,呵呵,好了先让我们来看看实现的效果截图:

好了,效果就是这样了,虽然实现了基本功能,但是还是不完美,比如说好友面板不能随着窗口改变大小而改变,不过应该已经可以解决了,只是还没实践,下面就发上源代码:

一共有三个类:

MemberModel.java //显示类QQ似好友标签信息的模板类;

TestPane.java    //添加多个模板的容器类;

TestFrame.java   //窗体类;

核心部分是前面两个类,注意,第二个类的布局方式不能为空,否则不能是容器显示滚动条,这是实现这个面板功能的一个难题,因为我不怎么会使用其他的布局方式,就只经常用空布局,经过这次才知道,光用空布局是不行的,空布局的应用受到了太多的限制。

在TestPane.java中添加好友信息的方式应该是代码添加黑名单信息的方式,可在其他类中调用方法,传递参数(数组)过来,然后对信息模板进行初始化,还有面板上的信息模板从程序刚开始运行就生成了,只是设置了不可见,后点击相关分类标签后的可视与不可视是同理的,也就是说你有多少个好友就一直会有多少个JLabel存在你的面板上,只是你可能看不见他们,我感觉自己的实现方式不是很好。还有在我所用的这种布局方式中,显示Label的宽度是一定的,我不知道怎么设置他的宽度和高度,所以通过在Label上添加了一个白色图标(185 X 60)来使Label变宽,然后再添加模板JPanel,如果网友们还有好的解决方法,希望不吝赐教。

下面是源码:

package coolbaby6;

import javax.swing.ImageIcon;

import javax.swing.JPanel;

import javax.swing.JButton;

import java.awt.Color;

import java.awt.Rectangle;

import javax.swing.JLabel;

import java.awt.Dimension;

import java.awt.Font;

public class MemberModel{

private static final long serialVersionUID = 1L;

public JButton jButton = null;//显示好友头像;

public JPanel jPanel = new JPanel();//模板容器;

private JLabel lb_nickName = null;//显示昵称;

private int pic;

private String nickname = null;

private JLabel lb_mood = null;//显示心情;

public MemberModel(int pic, String nickname, int len) {

super();

this.pic = pic;//头像编(有多种方法可以实现,这种最简单)

this.nickname = nickname;//昵称;

initialize();

}

private void initialize() {

lb_mood = new JLabel();

lb_mood.setBounds(new Rectangle(51, 30, 131, 20));

lb_mood.setFont(new Font("Dialog", Font.PLAIN, 12));

lb_mood.setText("世界上最遥远的距离不是生与死,而是我站在你面前你却不知道我爱你!");

lb_mood.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseEntered(java.awt.event.MouseEvent e) {

exchangeEnter();

lb_mood.setToolTipText(lb_mood.getText());

}

public void mouseExited(java.awt.event.MouseEvent e) {

exchangeExited();

}

});

lb_nickName = new JLabel();

lb_nickName.setBounds(new Rectangle(52, 10, 80, 20));

lb_nickName.setFont(new Font("Dialog", Font.BOLD, 14));

lb_nickName.setText(nickname);

jPanel.setSize(new Dimension(185, 60));

jPanel.setLayout(null);

jPanel.add(getJButton(), null);

jPanel.add(lb_nickName, null);

jPanel.add(lb_mood, null);

jPanel.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseExited(java.awt.event.MouseEvent e) {

exchangeExited();//鼠标移出模板区,改变背景颜色;

}

public void mouseEntered(java.awt.event.MouseEvent e) {

exchangeEnter();//鼠标移进模板区,改变背景颜色;

}

});

}

private void exchangeEnter() {

jPanel.setBackground(new Color(192,224,248));

}

private void exchangeExited() {

jPanel.setBackground(null);

}

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButton.setBounds(new Rectangle(8, 10, 40, 40));

jButton.setBackground(new Color(236, 255, 236));

jButton.setIcon(new ImageIcon(pic + ".jpg"));

jButton.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseExited(java.awt.event.MouseEvent e) {

exchangeExited();//鼠标移出模板区,改变背景颜色;

}

public void mouseEntered(java.awt.event.MouseEvent e) {

exchangeEnter();//鼠标移进模板区,改变背景颜色;

}

});

}

return jButton;

}

}

package coolbaby6;

import javax.swing.BorderFactory;

import javax.swing.JPanel;

import javax.swing.JLabel;

import javax.swing.BoxLayout;

import javax.swing.ImageIcon;

public class TestPane extends JPanel {

private static final long serialVersionUID = 1L;

private JLabel jLabel = null;

private JLabel jLabel1 = null;

private JLabel jLabel11 = null;

private JLabel jLabel12 = null;

private int clickF=0;

private int clickB=0;

public TestPane() {

super();

initialize();

}

private void initialize() {

jLabel12 = new JLabel();

jLabel12.setIcon(new ImageIcon("img/bg.jpg"));

jLabel12.add(new MemberModel(3,"CoolBabY3",200).jPanel);

jLabel12.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jLabel11 = new JLabel();

jLabel11.setIcon(new ImageIcon("img/bg.jpg"));

jLabel11.add(new MemberModel(2,"CoolBabY2",200).jPanel);

jLabel11.setBorder(BorderFactory.cxlCBUKwefjreateEmptyBorder(2, 2, 2, 2));

jLabel1 = new JLabel();

jLabel1.setIcon(new ImageIcon("img/bg.jpg"));

jLabel1.add(new MemberModel(1,"CoolBabY1",200).jPanel);

jLabel1.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jLabehttp://l = new JLabel();

jLabel.setText("我的好友");

jLabel.setIcon(new ImageIcon("img/ico.jpg"));

jLabel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jLabel.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

clickF+=1;

if(clickF%2==1){

jLabel1.setVisible(false);

jLabel11.setVisible(false);

jLabel12.setVisible(false);

jLabel.setIcon(new ImageIcon("img/ico2.jpg"));

update();

}else{

jLabel1.setVisible(true);

jLabel11.setVisible(true);

jLabel12.setVisible(true);

jLabel.setIcon(new ImageIcon("img/ico.jpg"));

update();

}

}

});

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

this.setSize(200, 408);

this.setLocation(20, 5);

this.add(jLabel, null);

this.add(jLabel1, null);

this.add(jLabel11, null);

this.add(jLabel12, null);

addJLabel();

}

private void update(){//更新UI界面;

this.updateUI();

}

private void clickBlack2(JLabel []jb){//点击标签,将后面的标签全部设为不可视;

for(int i=1;i

try{

jb[i].setVisible(false);

}catch(Exception e){

e.printStackTrace();

}

}

update();

}

private void clickBlack(JLabel []jb){//点击标签,将后面的标签全部设为可视;

for(int i=1;i

try{

jb[i].setVisible(true);

}catch(Exception e){

e.printStackTrace();

}

}

update();

}

private void addJLabel(){//添加黑名单的内容;

final JLabel []jb=new JLabel[7];

jb[0] = new JLabel();

jb[0].setText("黑名单");

jb[0].setIcon(new ImageIcon("img/ico2.jpg"));

jb[0].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[0].addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

clickB+=1;

if(clickB%2==1){

clickBlack(jb);

jb[0].setIcon(new ImageIcon("img/ico.jpg"));

}else{

clickBlack2(jb);

jb[0].setIcon(new ImageIcon("img/ico2.jpg"));

}

}

});

this.add(jb[0],null);

for(int i=1;i

jb[i]=new JLabel();

jb[i].setIcon(new ImageIcon("img/bg.jpg"));

jb[i].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[i].add(new MemberModel((i+3),"CoolBabY"+(i+3),200).jPanel);

jb[i].setVisible(false);

this.add(jb[i],null);

}

}

}

package coolbaby6;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.jscrollPane;

import javax.swing.ScrollPaneConstants;

import javax.swing.SwingUtilities;

public class TestFrame {

private JFrame jFrame = null;

private JPanel jContentPane = null;

private JScrollPane scrollPane=null;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

TestFrame application = new TestFrame();

application.getJFrame().setVisible(true);

}

});

}

private JFrame getJFrame() {

if (jFrame == null) {

jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setSize(230, 700);

jFrame.setTitle("模拟实现QQ面板功能");

jFrame.setContentPane(getJContentPane());

}

return jFrame;

}

private JScrollPane getScrollPane(){//给添加好友的容器JPanel添加滚动条;

if(scrollPane==null){

scrollPane=new JScrollPane(new TestPane());

//scrollPane.setBounds(20,5, -1, 600);

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );//不显示水平滚动条;

}

return scrollPane;

}

private JPanel getJContentPane() {//实例化底层的容器JPanel;

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getScrollPane(), BorderLayout.CENTER);

}

return jContentPane;

}

}

能成功编译成功我的源代码的方略: 

①、建一个项目,按我的类名建3个类,将源代码拷贝进去; 

②、在项目根目录下建一个名为img的文件夹,将要用到的图片放进去。(相关图片不再上传了,自己随便找几个图片测试即可) 

③、编译实现;

try{

jb[i].setVisible(false);

}catch(Exception e){

e.printStackTrace();

}

}

update();

}

private void clickBlack(JLabel []jb){//点击标签,将后面的标签全部设为可视;

for(int i=1;i

try{

jb[i].setVisible(true);

}catch(Exception e){

e.printStackTrace();

}

}

update();

}

private void addJLabel(){//添加黑名单的内容;

final JLabel []jb=new JLabel[7];

jb[0] = new JLabel();

jb[0].setText("黑名单");

jb[0].setIcon(new ImageIcon("img/ico2.jpg"));

jb[0].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[0].addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

clickB+=1;

if(clickB%2==1){

clickBlack(jb);

jb[0].setIcon(new ImageIcon("img/ico.jpg"));

}else{

clickBlack2(jb);

jb[0].setIcon(new ImageIcon("img/ico2.jpg"));

}

}

});

this.add(jb[0],null);

for(int i=1;i

jb[i]=new JLabel();

jb[i].setIcon(new ImageIcon("img/bg.jpg"));

jb[i].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[i].add(new MemberModel((i+3),"CoolBabY"+(i+3),200).jPanel);

jb[i].setVisible(false);

this.add(jb[i],null);

}

}

}

package coolbaby6;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.jscrollPane;

import javax.swing.ScrollPaneConstants;

import javax.swing.SwingUtilities;

public class TestFrame {

private JFrame jFrame = null;

private JPanel jContentPane = null;

private JScrollPane scrollPane=null;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

TestFrame application = new TestFrame();

application.getJFrame().setVisible(true);

}

});

}

private JFrame getJFrame() {

if (jFrame == null) {

jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setSize(230, 700);

jFrame.setTitle("模拟实现QQ面板功能");

jFrame.setContentPane(getJContentPane());

}

return jFrame;

}

private JScrollPane getScrollPane(){//给添加好友的容器JPanel添加滚动条;

if(scrollPane==null){

scrollPane=new JScrollPane(new TestPane());

//scrollPane.setBounds(20,5, -1, 600);

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );//不显示水平滚动条;

}

return scrollPane;

}

private JPanel getJContentPane() {//实例化底层的容器JPanel;

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getScrollPane(), BorderLayout.CENTER);

}

return jContentPane;

}

}

能成功编译成功我的源代码的方略: 

①、建一个项目,按我的类名建3个类,将源代码拷贝进去; 

②、在项目根目录下建一个名为img的文件夹,将要用到的图片放进去。(相关图片不再上传了,自己随便找几个图片测试即可) 

③、编译实现;

try{

jb[i].setVisible(true);

}catch(Exception e){

e.printStackTrace();

}

}

update();

}

private void addJLabel(){//添加黑名单的内容;

final JLabel []jb=new JLabel[7];

jb[0] = new JLabel();

jb[0].setText("黑名单");

jb[0].setIcon(new ImageIcon("img/ico2.jpg"));

jb[0].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[0].addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

clickB+=1;

if(clickB%2==1){

clickBlack(jb);

jb[0].setIcon(new ImageIcon("img/ico.jpg"));

}else{

clickBlack2(jb);

jb[0].setIcon(new ImageIcon("img/ico2.jpg"));

}

}

});

this.add(jb[0],null);

for(int i=1;i

jb[i]=new JLabel();

jb[i].setIcon(new ImageIcon("img/bg.jpg"));

jb[i].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[i].add(new MemberModel((i+3),"CoolBabY"+(i+3),200).jPanel);

jb[i].setVisible(false);

this.add(jb[i],null);

}

}

}

package coolbaby6;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.jscrollPane;

import javax.swing.ScrollPaneConstants;

import javax.swing.SwingUtilities;

public class TestFrame {

private JFrame jFrame = null;

private JPanel jContentPane = null;

private JScrollPane scrollPane=null;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

TestFrame application = new TestFrame();

application.getJFrame().setVisible(true);

}

});

}

private JFrame getJFrame() {

if (jFrame == null) {

jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setSize(230, 700);

jFrame.setTitle("模拟实现QQ面板功能");

jFrame.setContentPane(getJContentPane());

}

return jFrame;

}

private JScrollPane getScrollPane(){//给添加好友的容器JPanel添加滚动条;

if(scrollPane==null){

scrollPane=new JScrollPane(new TestPane());

//scrollPane.setBounds(20,5, -1, 600);

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );//不显示水平滚动条;

}

return scrollPane;

}

private JPanel getJContentPane() {//实例化底层的容器JPanel;

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getScrollPane(), BorderLayout.CENTER);

}

return jContentPane;

}

}

能成功编译成功我的源代码的方略: 

①、建一个项目,按我的类名建3个类,将源代码拷贝进去; 

②、在项目根目录下建一个名为img的文件夹,将要用到的图片放进去。(相关图片不再上传了,自己随便找几个图片测试即可) 

③、编译实现;

jb[i]=new JLabel();

jb[i].setIcon(new ImageIcon("img/bg.jpg"));

jb[i].setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

jb[i].add(new MemberModel((i+3),"CoolBabY"+(i+3),200).jPanel);

jb[i].setVisible(false);

this.add(jb[i],null);

}

}

}

package coolbaby6;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.jscrollPane;

import javax.swing.ScrollPaneConstants;

import javax.swing.SwingUtilities;

public class TestFrame {

private JFrame jFrame = null;

private JPanel jContentPane = null;

private JScrollPane scrollPane=null;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

TestFrame application = new TestFrame();

application.getJFrame().setVisible(true);

}

});

}

private JFrame getJFrame() {

if (jFrame == null) {

jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setSize(230, 700);

jFrame.setTitle("模拟实现QQ面板功能");

jFrame.setContentPane(getJContentPane());

}

return jFrame;

}

private JScrollPane getScrollPane(){//给添加好友的容器JPanel添加滚动条;

if(scrollPane==null){

scrollPane=new JScrollPane(new TestPane());

//scrollPane.setBounds(20,5, -1, 600);

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );//不显示水平滚动条;

}

return scrollPane;

}

private JPanel getJContentPane() {//实例化底层的容器JPanel;

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getScrollPane(), BorderLayout.CENTER);

}

return jContentPane;

}

}

能成功编译成功我的源代码的方略: 

①、建一个项目,按我的类名建3个类,将源代码拷贝进去; 

②、在项目根目录下建一个名为img的文件夹,将要用到的图片放进去。(相关图片不再上传了,自己随便找几个图片测试即可) 

③、编译实现;


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用Java实现小球碰壁反弹的简单实例(算法十分简单)
下一篇:详解Java如何实现基于Redis的分布式锁
相关文章

 发表评论

暂时没有评论,来抢沙发吧~