您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > Java模拟Windows开始菜单
packagewindowcomponents;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Font;importjava.awt.FontMetrics;importjava.awt.GradientPaint;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.Insets;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.geom.AffineTransform;importjava.awt.image.BufferedImage;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JMenuItem;importjavax.swing.JPanel;importjavax.swing.JPopupMenu;publicclassWindowsStartMenuSimulation{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubDemoWindow15dw=newDemoWindow15(模拟Windows开始菜单);dw.setBounds(dw.getToolkit().getScreenSize().width/3,dw.getToolkit().getScreenSize().height/3,dw.getToolkit().getScreenSize().width/3,dw.getToolkit().getScreenSize().height/3);dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);dw.setVisible(true);}}classDemoWindow15extendsJFrameimplementsActionListener{//菜单项图片ImageupdateImage=newImageIcon(images/update.gif).getImage();ImageprogramImage=newImageIcon(images/program.gif).getImage();ImagedocumentImage=newImageIcon(images/document.gif).getImage();ImagesetImage=newImageIcon(images/set.gif).getImage();ImagefindImage=newImageIcon(images/find.gif).getImage();ImagehelpImage=newImageIcon(images/help.gif).getImage();ImagerunImage=newImageIcon(images/run.gif).getImage();ImagelogoutImage=newImageIcon(images/logout.gif).getImage();ImageshutdownImage=newImageIcon(images/shutdown.gif).getImage();//按钮图片ImageIconwindowsImage=newImageIcon(images/windows.gif);//“开始”按钮JButtonjb=newJButton(开始,windowsImage);StartMenusm=newStartMenu();//布局面板JPaneljp1=newJPanel();JPaneljp2=newJPanel();ImageMenu1im=null;publicDemoWindow15(Stringtitle){super(title);jb.setFont(newFont(宋体,Font.BOLD,18));jp2.setLayout(newBorderLayout());jp2.add(jb,BorderLayout.WEST);add(jp1,BorderLayout.CENTER);add(jp2,BorderLayout.SOUTH);im=newImageMenu1(WindowsUpdate,updateImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(程序,programImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(文档,documentImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(设置,setImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(搜索,findImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(帮助和支持,helpImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(运行,runImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(注销,logoutImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);im=newImageMenu1(关机,shutdownImage);im.setFont(newFont(宋体,Font.BOLD,22));sm.add(im);//添加事件监听器jb.addActionListener(this);}publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstub//设置菜单可见,获得菜单的高度,便于下一步设置菜单的位置sm.setVisible(true);//显示图片菜单,通过位置设置,显示向上弹出的效果sm.show(jp1,0,jp1.getHeight()-sm.getHeight());}}classStartMenuextendsJPopupMenu{//显示WindowsXPprofessional的文字图片privateImageimage=null;publicStartMenu(){//创建文字图片image=createImage();}publicImagecreateImage(){Stringtext=WindowsXPProfessional;//创建缓冲图片类变量BufferedImagebi=newBufferedImage(30,400,BufferedImage.TYPE_INT_ARGB);//获取缓冲图片的2D绘图类变量Graphics2Dg2d=bi.createGraphics();//绘制渐变背景GradientPaintpaint=newGradientPaint(0,0,Color.black,100,100,Color.blue,true);g2d.setPaint(paint);g2d.fillRect(0,0,bi.getWidth(),bi.getHeight());//逆时针旋转绘制角度90度AffineTransformat=newAffineTransform();at.rotate(-Math.PI/2);g2d.setTransform(at);g2d.setColor(Color.white);g2d.setFont(newFont(宋体,Font.BOLD,16));//获取文字尺寸FontMetricsmetrics=g2d.getFontMetrics();intwidth=metrics.stringWidth(text);intheight=metrics.getAscent();//居中绘制文字g2d.drawString(text,-width-50,bi.getWidth()/2);returnbi;}publicInsetsgetInsets(){Insetsinsets=(Insets)super.getInsets().clone();insets.left+=image.getWidth(null);returninsets;}publicvoidpaint(Graphicsg){super.paint(g);if(image!=null){Insetsinsets=getInsets();g.drawImage(image,insets.left-image.getWidth(null),insets.top,null);}}}//图片菜单项类classImageMenu1extendsJMenuItem{privateImageimage=null;publicImageMenu1(Stringtitle,Imageimage){super(title);this.image=image;}//设置菜单项中显示文字图片的间隔大小publicInsetsgetInsets(){Insetsinsets=(Insets)super.getInsets().clone();insets.left+=image.getWidth(null);returninsets;}//重载绘图函数publicvoidpaint(Graphicsg){super.paint(g);if(image!=null){//获取间隔Insetsinsets=getInsets();g.drawImage(image,0,0,null);}}}
本文标题:Java模拟Windows开始菜单
链接地址:https://www.777doc.com/doc-2881134 .html