QQ Buddy list Tree-type list Java code implementation code _java

Source: Internet
Author: User

In the past when wandering around the internet to find a lot of people to use Java to achieve the QQ after landing panel problems feel very headache, recently, I was writing a simulation QQ project, it is indispensable to meet this problem, I Google in the Internet, Baidu, and finally found that a lot of people are troubled by this problem, But there is no solution, it is estimated that those who wrote out of the people, did not publish to the Internet, now, through their own aspects to find information, and finally wrote him out, but also do not waste last night, hehe, well, let's take a look at the results of the implementation of the screenshot:


Well, the effect is this, although the implementation of the basic functions, but still not perfect, such as the friend panel can not change the size of the window and change, but should already be resolved, but not practice, the following is sent on the source code:

A total of three categories:

Membermodel.java//Display class QQ like friend tag information template class;

Testpane.java//Add multiple templates to the container class;

Testframe.java//Form class;

The core part is the front two classes, note that the second class layout can not be empty, otherwise it can not be the container display scroll bar, which is the implementation of this panel function is a difficult problem, because I do not use the other layout, only often with empty layout, after this time to know that the light with empty layout is not, There are too many restrictions on the application of empty layouts.

The way to add friend information in Testpane.java should be the way code adds blacklist information, you can call methods in other classes, pass parameters (array), and then initialize the information template, and the information template on the panel is generated from the beginning of the program, just set the invisible, then click on the relevant category label after the visual and Not to be treated the same way, that is, how many friends you have will always have a number of JLabel on your panel, but you may not see them, I feel that their implementation is not very good. And in the way I use this layout, the display label width is certain, I do not know how to set his width and height, so by adding a white icon on the label (185 X 60) to widen the label, and then add Template JPanel, If the Netizen also has the good solution, hoped that does not hesitate to enlighten.

The following is the source code:

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;//display friend avatar; public JPanel JPanel = new JPanel ();//template container; private JLabel = null;

 Display nickname, private int pic;

 Private String nickname = null;
 Private JLabel Lb_mood = null;//display mood; public membermodel (int pic, String nickname, int len) {super ();
 This.pic = pic;//Avatar (There are many ways to achieve this simplest) This.nickname = 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 ("The furthest distance in the world is not between life and death, but when I stand in front of you and you don't know I love you!"); 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) {EX Changeexited ()//mouse out of the template area, change the background color;} public void mouseentered (Java.awt.event.MouseEvent e) {exchangeenter ()//mouse move into template
 area, change background color;});
 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 ()//mouse out of the template area, changing the background color;} public void mouseentered (Java.awt.event.MouseEvent e) {exchangeenter ();
 
 /mouse move into template area, change background color;});
 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"); 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"); jPanel);
 
 Jlabel11.setborder (Borderfactory.createemptyborder (2, 2, 2, 2));
 JLabel1 = new JLabel ();
 Jlabel1.seticon (New ImageIcon ("img/bg.jpg")); JLabel1.Add (New Membermodel (1, "CoolBabY1",) jPanel);
 
 Jlabel1.setborder (Borderfactory.createemptyborder (2, 2, 2, 2));
 JLabel = new JLabel ();
 Jlabel.settext ("My Best friend");
 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) {cl
  Ickf+=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 (This, Boxlayout.y_axis) (new boxlayout);
 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 () {//Update UI interface; This.updateui (); private void ClickBlack2 (JLabel []JB) {//click tag to set all subsequent tabs to be invisible; for (int i=1;i<jb.length;i++) {try{Jb[i].setvisib
  Le (false);
  }catch (Exception e) {e.printstacktrace ();
 } update (); private void Clickblack (JLabel []JB) {//click Tag, set all subsequent tabs to visual; for (int i=1;i<jb.length;i++) {try{jb[i].setvisible (t
  Rue);
  }catch (Exception e) {e.printstacktrace ();
 } update ();
 } private void Addjlabel () {//blacklist content added; final JLabel []jb=new jlabel[7];
 Jb[0] = new JLabel ();
 Jb[0].settext ("blacklist");
 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.length;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), 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 AP
  Plication = 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 ("Simulate the realization of QQ panel function");
 Jframe.setcontentpane (Getjcontentpane ());
 return jFrame; Private JScrollPane Getscrollpane () {//To add scroll bars to the container jpanel add buddy; if (scrollpane==null) {scrollpane=new JScrollPane (new
  Testpane ());
  Scrollpane.setbounds (20,5,-1, 600); ScrOllpane.sethorizontalscrollbarpolicy (scrollpaneconstants.horizontal_scrollbar_never);/do not show horizontal scroll bar;
 ScrollPane;
  Private JPanel Getjcontentpane () {//instantiate the underlying container JPanel; if (Jcontentpane = = null) {Jcontentpane = new JPanel ();
  Jcontentpane.setlayout (New BorderLayout ());
 Jcontentpane.add (Getscrollpane (), borderlayout.center);
 return jcontentpane;

 }
}

Successful compilation of my source code to successfully compile the strategy:
①, build a project, according to my class name to build 3 classes, the source code to copy in;
②, in the project root directory to build a folder called IMG, will be used to put in the picture. (related pictures are no longer uploaded, you can find a few pictures to test it)
③, compile and implement;

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.