📄 windowdestop.java
字号:
import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.net.*;import javax.swing.border.*;public class WindowDestop extends Applet { //菜单栏图标数组 ImageIcon[] icons = new ImageIcon[9]; //菜单栏图标的文件名 String[] iconFileNames = {"windowIcon.gif", "leftIcon.gif", "shutDownIcon.gif", "runIcon.gif", "helpIcon.gif", "searchIcon.gif", "setIcon.gif", "documentIcon.gif", "programIcon.gif"}; //桌面图标数组 ImageIcon[] destopIcons = new ImageIcon[3]; //桌面图标数组的文件名 String[] destopIconsFileNames = {"1.gif", "2.gif", "3.gif"}; JButton[] destopBtns = new JButton[3];//桌面按钮数组 String[] destopBtnTitles = {"程序1", "程序2", "程序3"};//控钮的标题 MenuPane menuPane;//菜单面板 JPanel destopPane = new JPanel(); //Window面板 JButton menuStart = new JButton();//开始按钮 JPanel panelBottom = new JPanel();//放置开始按钮的面板 //设置桌面底端面板的布局 FlowLayout flowLayout1 = new FlowLayout(); Dimension appletSize;//Applet的大小 //Applet的初始化方法 public void init() { try {jbInit();} catch(Exception e) {} } //被初始化方法引用的方法 private void jbInit() throws Exception { //取得图标的方法 for(int i = 0; i < 9; ++i){ icons[i] = new ImageIcon(new URL(this.getCodeBase(), iconFileNames[i])); } menuPane = new MenuPane(icons);//根据图标创建菜单面板 //设置开始按钮的代码 menuStart.setText("开始"); menuStart.setIcon(icons[0]); menuStart.setPreferredSize(new Dimension(60, 23)); menuStart.setBorder(BorderFactory.createEtchedBorder()); menuStart.setAlignmentY((float) 0.0); //为开始按钮加入按键事件,决定菜单面板的显示位置 menuStart.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if(menuPane.getY() == appletSize.height-285){ menuPane.setBounds(new Rectangle(3, appletSize.height-285+1800, 117, 250)); }else{ menuPane.setBounds(new Rectangle(3, appletSize.height-285, 117, 250)); } } }); this.setLayout(new BorderLayout()); //设置Applet的布局 destopPane.setLayout(null); //设置桌面面板的布局 destopPane.setBackground(new Color(98, 112, 200)); //为桌面面扳加入鼠标移动事件,当鼠标坐标x>205,y< appletSize.height-285时 //使菜单不可见 destopPane.addMouseMotionListener(new java.awt.event. MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { if(e.getX() > 120 || e.getY() < appletSize.height - 285) { menuPane.setBounds(new Rectangle(3, appletSize.height-285+1800, 117, 250)); } } }); destopPane.add(menuPane, null);//向桌面面板加入菜单面板 //设置桌面的控件 for(int i = 0; i < 3; ++i){ destopIcons[i] = new ImageIcon(new URL(this.getCodeBase(), destopIconsFileNames[i])); destopBtns[i] = new JButton(destopBtnTitles[i], destopIcons[i]); destopBtns[i].setVerticalTextPosition(SwingConstants.BOTTOM); destopBtns[i].setHorizontalTextPosition(SwingConstants.CENTER); destopBtns[i].setBounds(new Rectangle(i*75+3, 3, 68, 68)); destopPane.add(destopBtns[i],null); } panelBottom.setLayout(flowLayout1); //设置桌面底端面板的布局 //使桌面底端面板的布局向左对齐 flowLayout1.setAlignment(FlowLayout.LEFT); //设置桌面底端面板的边框 panelBottom.setBorder(BorderFactory.createRaisedBevelBorder()); //设置菜单面板的位置,当Applet启动时,使菜单不可见 appletSize = this.getSize(); menuPane.setBounds(new Rectangle(3, appletSize.height-285+1800, 117, 250)); panelBottom.add(menuStart, null); //向桌面底端面板加入开始按钮 this.add(destopPane, BorderLayout.CENTER);//向Applet加入桌面面板 this.add(panelBottom, BorderLayout.SOUTH);//向Applet加入桌面底端面板 } //创建菜单类 class MenuPane extends JPanel{ //按钮的标题 String[] btnTitle = {"关机", "运行", "帮助", "搜索", "设置", "文档", "程序"}; //声明菜单的按钮 MenuBtn[] menuBtns = new MenuBtn[7]; JLabel labelLeft = new JLabel(); //菜单左边的标签 JPanel paneRight = new JPanel(); //菜单右边的面板 public MenuPane(ImageIcon[] icons){ super();//执行面板的父构造方法 //设置菜单栏的边框与布局 this.setBorder(BorderFactory.createEtchedBorder()); this.setLayout(new BorderLayout()); //创建菜单的七个按钮 for(int i = 0; i < 7; ++i){ menuBtns[i] = new MenuBtn(btnTitle[i],icons[i+2]); } //向菜单的右边面板加入按钮 for(int i = 6; i >= 0; --i){ paneRight.add(menuBtns[i],null); } labelLeft.setIcon(icons[1]);//设置菜单左边的图标 //向菜单加入左边标签 this.add(labelLeft, BorderLayout.WEST); //向菜单加右边标签 this.add(paneRight, BorderLayout.CENTER); } } //创建菜单按钮类 class MenuBtn extends JButton implements MouseListener, ActionListener{ Border lineBorder; public MenuBtn(String title , Icon icon){ //执行父类的方法 super(title, icon); lineBorder = BorderFactory.createLineBorder(Color.lightGray,2); this.setBorder(lineBorder);//设置按钮的边框 this.setPreferredSize(new Dimension(80, 28));//设置按钮的大小 this.addMouseListener(this);//加入鼠标移动接收器 this.addActionListener(this);//加入按钮接收器 } //按钮的按键事件 public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"您好,我是" + this.getText() + "按钮"); } //当鼠标进入载有鼠标接收器的容器内时,该事件被激活 public void mouseEntered(MouseEvent e) { this.setBackground(new Color(98, 112, 200)); } //当鼠标离开载有鼠标接收器的容器内时,该事件被激活 public void mouseExited(MouseEvent e) { this.setBackground(new Color(212, 208, 200)); } //当鼠标按下时,该事件被激发 public void mousePressed(MouseEvent e) {} //当鼠标按下时,该事件被激发,但该事件在mousePressed事件之后 public void mouseReleased(MouseEvent e) {} //当鼠标按下时,该事件被激活,但该事件在mouseReleased事件之后 public void mouseClicked(MouseEvent e) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -