Java Swing编程之仿js树状折叠菜单
生活随笔
收集整理的這篇文章主要介紹了
Java Swing编程之仿js树状折叠菜单
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近要完成一個需求:用swing做個樹狀菜單,含二級菜單,點(diǎn)擊一級菜單展開二級菜單,且二級菜單數(shù)目超過預(yù)覽視圖會出現(xiàn)滾動條。由于swing研究的少,花了不少精力!
先看下測試效果圖:
收起圖:
展開圖:
完整源碼:
1 package com.xuwei.test2; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.GridLayout; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 9 import javax.swing.JButton; 10 import javax.swing.JFrame; 11 import javax.swing.JPanel; 12 import javax.swing.JScrollPane; 13 14 15 public class TestFrm4 extends JFrame{ 16 private JButton btn1,btn2,btn3,btn4,btn5; 17 private JPanel pNorth,pSouth,subMenuContainer; 18 private JScrollPane pCenter; 19 private JButton[] btn = null; 20 private static boolean expand=false; 21 22 public TestFrm4(){ 23 btn1=new JButton("Grade1 menu1"); 24 btn1.setBackground(Color.CYAN); 25 btn2=new JButton("Grade1 menu2"); 26 btn2.setBackground(Color.CYAN); 27 btn3=new JButton("Grade1 menu3"); 28 btn3.setBackground(Color.CYAN); 29 btn3.addActionListener(new ActionHandler()); 30 31 btn4=new JButton("Grade1 menu4"); 32 btn4.setBackground(Color.CYAN); 33 btn5=new JButton("Grade1 menu5"); 34 btn5.setBackground(Color.CYAN); 35 pNorth=new JPanel(); 36 pNorth.setLayout(new GridLayout(3,1)); 37 pSouth=new JPanel(); 38 pSouth.setLayout(new GridLayout(2,1)); 39 subMenuContainer=new JPanel(); 40 subMenuContainer.setLayout(new GridLayout(25,1)); 41 42 btn=new JButton[25]; 43 for(int i=0;i<btn.length;i++){ 44 btn[i]=new JButton("[菜單"+i+"]"); 45 btn[i].setBackground(Color.WHITE); 46 } 47 48 this.setLayout(new BorderLayout()); 49 50 pNorth.add(btn1); pNorth.add(btn2); pNorth.add(btn3); 51 for(int i=0;i<btn.length;i++){ 52 subMenuContainer.add(btn[i]); 53 } 54 pCenter=new JScrollPane(subMenuContainer); 55 56 pSouth.add(btn4);pSouth.add(btn5); 57 this.add(pNorth,"North"); 58 this.add(pCenter,"Center"); 59 this.add(pSouth,"South"); 60 61 this.setVisible(true); 62 this.setSize(500,600); 63 this.setResizable(false); 64 this.setLocationRelativeTo(null); 65 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 66 67 } 68 69 70 71 private class ActionHandler implements ActionListener{ 72 73 @Override 74 public void actionPerformed(ActionEvent e) { 75 if(btn3==e.getSource()){ 76 if(expand){//折疊 77 pNorth.setLayout(new GridLayout(3,1)); 78 pNorth.remove(btn4);pNorth.remove(btn5); 79 pSouth.add(btn4);pSouth.add(btn5); 80 for(int i=0;i<btn.length;i++){ 81 subMenuContainer.add(btn[i]); 82 } 83 validate(); 84 getContentPane().repaint(); 85 expand=false; 86 }else{//展開 87 for(int i=0;i<btn.length;i++){ 88 subMenuContainer.remove(btn[i]); 89 } 90 pSouth.removeAll(); 91 pNorth.setLayout(new GridLayout(5,1)); 92 pNorth.add(btn4); 93 pNorth.add(btn5); 94 pNorth.repaint(); 95 pCenter.repaint(); 96 pSouth.repaint(); 97 validate(); 98 getContentPane().repaint(); 99 expand=true; 100 } 101 } 102 } 103 104 } 105 106 public static void main(String[] args) { 107 108 new TestFrm4(); 109 } 110 111 }這里頻繁添加刪除組件需要及時刷新界面,swing有幾個方法要反復(fù)調(diào)用:
repaint(),validate(),invalidate(),doLayout().
之前我由于沒調(diào)用validate()導(dǎo)致界面刷新出現(xiàn)很多問題!
swing要仔細(xì)研究發(fā)現(xiàn)東西也不少!
?
轉(zhuǎn)載于:https://www.cnblogs.com/davidxu/p/4465651.html
總結(jié)
以上是生活随笔為你收集整理的Java Swing编程之仿js树状折叠菜单的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决WORDPRESS评论时头像不显示的
- 下一篇: Nginx 备战-优化指南