When I was wandering around the Internet, I found that many people were very troubled by the problem of using Java to implement the panel after QQ login. Recently, I was writing projects that simulate QQ, so I encountered this problem indispensable. I went to Google and Baidu online. Finally, I found that many people were troubled by this problem but had no solution. It is probably those who wrote it and did not post it online. Now, after searching for information from multiple aspects, I finally wrote it out. It is not in vain that I stayed up late last night. Haha, let's take a look at the screenshot of the implementation effect:
OK, this is the effect. Although the basic functions are implemented, it is still imperfect. For example, the friend panel cannot be changed as the window changes in size, but it should have been solved. It is not yet practiced. Let’s post the source code below:
There are three categories in total:
MemberModel.java //Template class that displays QQ-like friend tag information;
TestPane.java // Add container class to multiple templates;
TestFrame.java //Form class;
The core part is the first two classes. Note that the layout method of the second class cannot be empty, otherwise it cannot be a container display scroll bar. This is a problem in implementing this panel function, because I don’t know how to use other layout methods, so I only use empty layout frequently. After this time, I realized that using empty layout alone is not possible, and the application of empty layout is too restricted.
The way to add friend information in TestPane.java should be the way to add blacklist information in the code. You can call methods in other classes, pass parameters (arrays), and then initialize the information template. The information template on the panel is generated from the beginning of the program, but it is set to be invisible. After clicking the relevant classification label, the visualization and invisible are the same. That is to say, as many friends you have, there will always be JLabels on your panel, but you may not see them. I feel that my implementation method is not very good. In the layout method I use, the width of the Label is certain. I don’t know how to set its width and height, so I add a white icon (185 X 60) to the Label to make the Label wider, and then add the template JPanel. If netizens have a good solution, I hope I will give you some advice.
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;//Show friend avatar; public JPanel jPanel = new JPanel();//Template container; private JLabel lb_nickName = null;//Show nickname; private int pic; private String nickname = null; private JLabel lb_mood = null;//Show mood; public MemberModel(int pic, String nickname, int len) { super(); this.pic = pic;//Avatar compilation (there are many ways to implement, this is the 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 farthest distance in the world is not life and death, but I stand in front of you and you don't know that 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) { exchangeExited();//Mouse move out of the template area and change the background color; } public void mouseEntered(java.awt.event.MouseEvent e) { exchangeEnter();//Move the mouse into the template area and change the 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 move out of the template area and change the background color; } public void mouseEntered(java.awt.event.MouseEvent e) { exchangeEnter();//Move the mouse into the template area and change the 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",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.createEmptyBorder(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)); jLabel = new JLabel(); jLabel.setText("My 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) { 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(){//Update the UI interface; this.updateUI(); } private void clickBlack2(JLabel []jb){//Click on the tags and set all the following tags to be invisible; for(int i=1;i<jb.length;i++){ try{ jb[i].setVisible(false); } catch(Exception e){ e.printStackTrace(); } } update(); } private void clickBlack(JLabel []jb){//Click on the tags and set all the following tags to be visible; for(int i=1;i<jb.length;i++){ try{ jb[i].setVisible(true); } catch(Exception e){ e.printStackTrace(); } } update(); } private void addJLabel(){//Add blacklist content; 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),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("Simulate to implement QQ panel functions"); jFrame.setContentPane(getJContentPane()); } return jFrame; } private JScrollPane getScrollPane(){//Add scrollbar to the container JPanel that adds friends; if(scrollPane==null){ scrollPane=new JScrollPane(new TestPane()); //scrollPane.setBounds(20,5, -1, 600); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );//Do not display horizontal scrollbars; } return 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; }} A strategy to successfully compile my source code:
①. Build a project, build 3 categories according to my class name, and copy the source code in;
②. Create a folder called img in the root directory of the project and put the pictures to be used in it. (The related pictures are no longer uploaded, just find a few pictures to test)
③, compile and implement;
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.