package windowcomponents;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.GradientPaint;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Insets;import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JPopupMenu;public class WindowsStartMenuSimulation {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubDemoWindow15 dw = new DemoWindow15("模拟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);}}class DemoWindow15 extends JFrame implements ActionListener {// 菜单项图片Image updateImage = new ImageIcon("images/update.gif").getImage();Image programImage = new ImageIcon("images/program.gif").getImage();Image documentImage = new ImageIcon("images/document.gif").getImage();Image setImage = new ImageIcon("images/set.gif").getImage();Image findImage = new ImageIcon("images/find.gif").getImage();Image helpImage = new ImageIcon("images/help.gif").getImage();Image runImage = new ImageIcon("images/run.gif").getImage();Image logoutImage = new ImageIcon("images/logout.gif").getImage();Image shutdownImage = new ImageIcon("images/shutdown.gif").getImage();// 按钮图片ImageIcon windowsImage = new ImageIcon("images/windows.gif");// “开始”按钮JButton jb = new JButton("开始", windowsImage);StartMenu sm = new StartMenu();// 布局面板JPanel jp1 = new JPanel();JPanel jp2 = new JPanel();ImageMenu1 im = null;public DemoWindow15(String title) {super(title);jb.setFont(new Font("宋体", Font.BOLD, 18));jp2.setLayout(new BorderLayout());jp2.add(jb, BorderLayout.WEST);add(jp1, BorderLayout.CENTER);add(jp2, BorderLayout.SOUTH);im = new ImageMenu1("Windows Update", updateImage); im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("程序", programImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("文档", documentImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("设置", setImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("搜索", findImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("帮助和支持", helpImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("运行", runImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("注销", logoutImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);im = new ImageMenu1("关机", shutdownImage);im.setFont(new Font("宋体", Font.BOLD, 22));sm.add(im);// 添加事件监听器jb.addActionListener(this);}public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub// 设置菜单可见,获得菜单的高度,便于下一步设置菜单的位置sm.setVisible(true);// 显示图片菜单,通过位置设置,显示向上弹出的效果sm.show(jp1, 0, jp1.getHeight() - sm.getHeight());}}class StartMenu extends JPopupMenu {// 显示Windows XP professional的文字图片private Image image = null;public StartMenu() {// 创建文字图片image = createImage();}public Image createImage() {String text = "Windows XP Professional";// 创建缓冲图片类变量BufferedImage bi = new BufferedImage(30, 400,BufferedImage.TYPE_INT_ARGB);// 获取缓冲图片的2D绘图类变量Graphics2D g2d = bi.createGraphics();// 绘制渐变背景GradientPaint paint = new GradientPaint(0, 0, Color.black, 100, 100,Color.blue, true);g2d.setPaint(paint);g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());// 逆时针旋转绘制角度90度AffineTransform at = new AffineTransform();at.rotate(-Math.PI / 2);g2d.setTransform(at);g2d.setColor(Color.white);g2d.setFont(new Font("宋体", Font.BOLD, 16));// 获取文字尺寸FontMetrics metrics = g2d.getFontMetrics();int width = metrics.stringWidth(text);int height = metrics.getAscent();// 居中绘制文字g2d.drawString(text, -width - 50, bi.getWidth() / 2);return bi;}public Insets getInsets() {Insets insets = (Insets) super.getInsets().clone();insets.left += image.getWidth(null);return insets;}public void paint(Graphics g) {super.paint(g);if (image != null) {Insets insets = getInsets();g.drawImage(image, insets.left - image.getWidth(null), insets.top,null);}}}// 图片菜单项类class ImageMenu1 extends JMenuItem {private Image image = null;public ImageMenu1(String title, Image image) {super(title);this.image = image;}// 设置菜单项中显示文字图片的间隔大小public Insets getInsets() {Insets insets = (Insets) super.getInsets().clone();insets.left += image.getWidth(null);return insets;}// 重载绘图函数public void paint(Graphics g) {super.paint(g);if (image != null) {// 获取间隔Insets insets = getInsets();g.drawImage(image, 0, 0, null);}}}。