jnmenubar.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 208 行

JAVA
208
字号
package org.jnode.wt.components;

import org.jnode.wt.desktop.JNDesktop;
import org.jnode.wt.events.JNActionEvent;
import org.jnode.wt.layouts.JNFlowLayout;
import org.jnode.wt.events.JNActionListener;

import java.awt.Color;
import java.awt.Insets;
import java.awt.Menu;
import java.awt.peer.MenuBarPeer;
import java.util.ArrayList;

//import java.awt.event.*;


/**
 * @author Kishore
 */
public class JNMenuBar extends JNPanel implements MenuBarPeer, JNActionListener {

    private ArrayList menus = new ArrayList();

    private JNMenu currentlyShowing_menu = null;

    JNList alist = new JNList();


    /**
     * JNMenuBar constructor comment.
     */
    public JNMenuBar() {
        super();

        init();
    }

    public void actionPerformed(JNActionEvent ae) {

        String menutitle = ae.getActionCommand();

        JNMenu amenu = null;

        int index = 0;
        for (; index < menus.size(); index++) {
            amenu = (JNMenu) menus.get(index);

            if (amenu.getLabel().equals(menutitle)) {
                break;
            }
        }
        //++++++++++++++++++++++++++++++++++++++++++++

        // # [1] if the same menu is already shown ,remove and return.
        //if(currentlyShowing_menu.equals( amenu ))
        if (currentlyShowing_menu == amenu) {
            setVisibleMenu(false, amenu);
        } else {
            // remove the previous one and show the new one.
            setVisibleMenu(false, currentlyShowing_menu);
            setVisibleMenu(true, amenu);
        }


        /*
            # [1] Remove if the same menu is already shown
            if( already_shown_menu == is same as this)
            {
                remove this and
                return;
            }

            # [2]
            if(already_shown_menu is different than this)
            {
                remove the previous menu,
                and show the new menu.
            }
        */

    }

    public void add(JNMenu menu) {
        if (null != menu) {
            JNButton menubutton = menu.getMenuTitleButton();
            menu.setMenuBar(this);

            menubutton.setMouseOverEffect(true);

            menubutton.addActionListener(this);
            super.add(menubutton);

            menubutton.setSize(menubutton.getPreferredSize());

//		System.out.println(menubutton.getText()+"   "+menubutton.getWidth() +","+ menubutton.getHeight());

            /* Note: super is set to Flow Layout */
//		int x = (int)(menubutton.getLocation().getX());

//		int y = (int)(menubutton.getAbsLocation().getY()) + menubutton.getHeight();

            // [0] calculate the absoloute location of menu, with ref to Desktop.
            // [1] set the Location of MEnu

//		menu.setLocation(x,y);

            menus.add(menu);
        }

    }

    private void init() {
        this.setInsets(new Insets(2, 2, 2, 2));
        setLayout(new JNFlowLayout());
    }

    public void internallyPaint(java.awt.Graphics g) {
        super.internallyPaint(g);

        g.setColor(Color.black);
        g.drawRect(0, 0, this.getWidth(), this.getHeight());


    }

    public void remove(JNMenu menu) {
        if (null != menu) {
            menus.remove(menu);

            JNButton menubutton = menu.getMenuTitleButton();
            menubutton.removeActionListener(this);
            super.remove(menubutton);

            /* Remove JNMenu from LAyeredPanel */
        }
    }

    public void setVisible(boolean b) {
        super.setVisible(b);


        if (b == false) {
//		if( isPopupVisible )
            {
                setVisibleMenu(false, currentlyShowing_menu);
//			isPopupVisible = false;
            }
        }

    }

    protected void setVisibleMenu(boolean visible, JNMenu menu) {
        JNDesktop desktop = getDesktopManager().getCurrentDesktop();

        if (true == visible) {
            JNButton menubutton = menu.getMenuTitleButton();

            int x = (int) (menubutton.getAbsLocation().getX());
            int y = (int) (menubutton.getAbsLocation().getY()) + menubutton.getHeight();

            // [0] calculate the absoloute location of menu, with ref to Desktop.
            // [1] set the Location of MEnu
            menu.setLocation(x, y);

            // [2] Add the menu to LayeredPAnel.
            desktop.add(menu, org.jnode.wt.components.JNLayeredContainer.MENU_COMBO_LAYER);
            // [3] setVisible of menu to true
            menu.setVisible(true);

/*		alist.setLocation(x,y);
		desktop.add(alist, JNLayeredContainer.MENU_COMBO_LAYER );
		alist.setVisible(true);
		*/

            currentlyShowing_menu = menu;
        } else {

            if (menu != null) {
                // [2] Remove the menu to LayeredPAnel.
                desktop.remove(menu, org.jnode.wt.components.JNLayeredContainer.MENU_COMBO_LAYER);
                // [3] setVisible of menu to false
                menu.setVisible(false);

/*			desktop.remove(alist, JNLayeredContainer.MENU_COMBO_LAYER );
			alist.setVisible(false);
			*/
                getParent().repaint();

            }

            currentlyShowing_menu = null;
        }

    }

    public void addHelpMenu(Menu menu) {
        //TODO: implement it
    }

    public void addMenu(Menu menu) {
        //TODO: implement it
    }

    public void delMenu(int index) {
        //TODO: implement it
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?