本篇文章给大家分享的内容是Java之仿js实现树状折叠菜单,有着一定的参考价值,有需要的朋友可以参考一下
代码
package com.tuzi;import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; public class B extends JFrame{ private JButton btn1,btn2,btn3,btn4,btn5; private JPanel pNorth,pSouth,subMenuContainer; private JScrollPane pCenter; private JButton[] btn = null; private static boolean expand=false; public B(){ btn1=new JButton("Grade1 menu1"); btn1.setBackground(Color.CYAN); btn2=new JButton("Grade1 menu2"); btn2.setBackground(Color.CYAN); btn3=new JButton("Grade1 menu3"); btn3.setBackground(Color.CYAN); btn3.addActionListener(new ActionHandler()); btn4=new JButton("Grade1 menu4"); btn4.setBackground(Color.CYAN); btn5=new JButton("Grade1 menu5"); btn5.setBackground(Color.CYAN); pNorth=new JPanel(); pNorth.setLayout(new GridLayout(3,1)); pSouth=new JPanel(); pSouth.setLayout(new GridLayout(2,1)); subMenuContainer=new JPanel(); subMenuContainer.setLayout(new GridLayout(25,1)); btn=new JButton[25]; for(int i=0;i<btn.length;i++){ btn[i]=new JButton("[菜单"+i+"]"); btn[i].setBackground(Color.WHITE); } this.setLayout(new BorderLayout()); pNorth.add(btn1); pNorth.add(btn2); pNorth.add(btn3); for(int i=0;i<btn.length;i++){ subMenuContainer.add(btn[i]); } pCenter=new JScrollPane(subMenuContainer); pSouth.add(btn4);pSouth.add(btn5); this.add(pNorth,"North"); this.add(pCenter,"Center"); this.add(pSouth,"South"); this.setVisible(true); this.setSize(500,600); this.setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class ActionHandler implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { if(btn3==e.getSource()){ if(expand){//折叠 pNorth.setLayout(new GridLayout(3,1)); pNorth.remove(btn4);pNorth.remove(btn5); pSouth.add(btn4);pSouth.add(btn5); for(int i=0;i<btn.length;i++){ subMenuContainer.add(btn[i]); } validate(); getContentPane().repaint(); expand=false; }else{//展开 for(int i=0;i<btn.length;i++){ subMenuContainer.remove(btn[i]); } pSouth.removeAll(); pNorth.setLayout(new GridLayout(5,1)); pNorth.add(btn4); pNorth.add(btn5); pNorth.repaint(); pCenter.repaint(); pSouth.repaint(); validate(); getContentPane().repaint(); expand=true; } } } } public static void main(String[] args) { new B(); } }
效果图
以上是Java之仿js实现树状折叠菜单的详细内容。更多信息请关注PHP中文网其他相关文章!

Java在不同操作系统上的表现存在细微差异。1)JVM实现不同,如HotSpot、OpenJDK,影响性能和垃圾回收。2)文件系统结构和路径分隔符不同,需使用Java标准库处理。3)网络协议实现差异影响网络性能。4)GUI组件外观和行为在不同系统上有别。通过使用标准库和虚拟机测试,可减少这些差异的影响,确保Java程序稳定运行。

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,对象,继承,多态性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允许theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

Java'splatFormIndependecemeanSapplicationsCanrunonAnyPlatFormWithAjvm,使“ Writeonce,RunanyWhere”。

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。