QQ石友列表樹形列表java代碼完成代碼。本站提示廣大學習愛好者:(QQ石友列表樹形列表java代碼完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是QQ石友列表樹形列表java代碼完成代碼正文
之前在網上瞎轉游的時刻就發明許多工資用Java完成QQ上岸後的面板的成績覺得非常頭疼,比來我因在寫模仿QQ的項目,故弗成或缺的碰到了這一個成績,在網上我谷歌了,百度了,終究發明的是有許多人被這一成績困擾,卻沒有處理的計劃,估量是那些寫出來了的人,沒有宣布到網下去,現在,經由本身的多方面查找材料,終究把他寫出來了,也不枉昨晚熬夜了,呵呵,好了先讓我們來看看完成的後果截圖:
好了,後果就是如許了,固然完成了根本功效,然則照樣不完善,好比說石友面板不克不及跟著窗口轉變年夜小而轉變,不外應當曾經可以處理了,只是還沒理論,上面就發上源代碼:
一共有三個類:
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.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("我的石友"); 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<jb.length;i++){ try{ jb[i].setVisible(false); }catch(Exception e){ e.printStackTrace(); } } update(); } private void clickBlack(JLabel []jb){//點擊標簽,將前面的標簽全體設為可視; for(int i=1;i<jb.length;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.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("模仿完成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的文件夾,將要用到的圖片放出來。(相干圖片不再上傳了,本身隨意找幾個圖片測試便可)
③、編譯完成;
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。