⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bqcommandbar.java

📁 J2me唆哈的代码
💻 JAVA
字号:
/*
 * Created on 2005-9-20 by pcy
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package a.a.a.midp.lcdui;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.microedition.lcdui.*;

import a.a.a.b.*;

import java.awt.*;

public class BQCommandBar extends JMenuBar {
	
    public static final long serialVersionUID=1;

    BQDisplayable displayable;

    JMenu leftMenu;
    JMenu rightMenu;
    
    private BQCommand[] leftCommands;
    private int leftCommandNums;
    private BQCommand[] rightCommands;
    private int rightCommandNums;
    private BQMouseAdapter leftMouseAdapter;
    private BQMouseAdapter rightMouseAdapter;

    public BQCommandBar(BQDisplayable p) {
        super();
        displayable=p;
        this.setBackground(BQDisplayable.BACKGROUND_COLOR);
        //this.setBorder(LineBorder.createBlackLineBorder());
        
        leftMenu=new JMenu();
        rightMenu=new JMenu();
        this.setLayout(null);
        this.add(leftMenu);
        leftMenu.setHorizontalAlignment(JLabel.LEADING);
        this.add(rightMenu);
        rightMenu.setHorizontalAlignment(JLabel.CENTER);
        
    }
    
    public void addCommand(BQCommand cmd){
        if(isLeftCommand(cmd)){
            addLeftCommand(cmd);
        }else{
            addRightCommand(cmd);
        }
    }
    
    public void removeCommand(BQCommand cmd){

        if(isLeftCommand(cmd)){
            removeLeftCommand(cmd);
        }else{
            removeRightCommand(cmd);
        }
    }
    
    private boolean isLeftCommand(BQCommand cmd){
        int type=cmd.getCommandType();
        return (type!=Command.EXIT
                &&type!=Command.BACK
                &&type!=Command.CANCEL
                &&type!=Command.STOP
                &&type!=Command.ITEM);
    }
    
    private void addLeftCommand(BQCommand cmd){
        for (int i = 0; i < leftCommandNums; ++i) {
            if (leftCommands[i] == cmd) {
                return;
            }
        }
        if ((leftCommands == null) || (leftCommandNums == leftCommands.length)) {
            BQCommand[] newCommands = new BQCommand[leftCommandNums + 4];
            if (leftCommands != null) {
                System.arraycopy(leftCommands, 0, newCommands, 0, leftCommandNums);
            }
            leftCommands = newCommands;
        }
        leftCommands[leftCommandNums] = cmd;
        ++leftCommandNums;
        buildLeftMenu(cmd);
    }
    
    private void addRightCommand(BQCommand cmd){
        for (int i = 0; i < rightCommandNums; ++i) {
            if (rightCommands[i] == cmd) {
                return;
            }
        }
        if ((rightCommands == null) || (rightCommandNums == rightCommands.length)) {
            BQCommand[] newCommands = new BQCommand[rightCommandNums + 4];
            if (rightCommands != null) {
                System.arraycopy(rightCommands, 0, newCommands, 0, rightCommandNums);
            }
            rightCommands = newCommands;
        }
        rightCommands[rightCommandNums] = cmd;
        ++rightCommandNums;
        buildRightMenu(cmd);
    }
    
    private void removeLeftCommand(BQCommand cmd){
        for (int i = 0; i < leftCommandNums; ++i) {
            if (leftCommands[i] == cmd) {
                leftMenu.remove(i);
                --leftCommandNums;
                System.arraycopy(leftCommands,i+1,leftCommands,i,leftCommandNums-i);
                leftCommands[leftCommandNums] = null;
                if(leftCommandNums<=1){
                    buildLeftMenu(leftCommands[0]);
                }
                break;
            }
        }
    }
    
    private void removeRightCommand(BQCommand cmd){
        for (int i = 0; i < rightCommandNums; ++i) {
            if (rightCommands[i] == cmd) {
                rightMenu.remove(i);
                --rightCommandNums;
                System.arraycopy(rightCommands,i+1,rightCommands,i,rightCommandNums-i);
                //rightCommands[i] = rightCommands[--rightCommandNums];
                rightCommands[rightCommandNums] = null;
                if(rightCommandNums<=1){
                    buildRightMenu(rightCommands[0]);
                }
                break;
            }
        }
    }
    
    public int getLeftSoftCmdCnt(){
        return leftCommandNums;
    }
    
    public int getRightSoftCmdCnt(){
        return rightCommandNums;
    }
    
    public int getCommandCount(){
        return leftCommandNums+rightCommandNums;
    }
    
    private void buildLeftMenu(BQCommand cmd){
        if(leftCommandNums==0){
            leftMenu.setVisible(false);
        }if(leftCommandNums==1){
            leftMenu.setText(cmd.getLabel());
            if(leftMouseAdapter==null){
                leftMouseAdapter=new BQMouseAdapter(displayable,leftMenu);
            }else{
                leftMenu.removeMouseListener(leftMouseAdapter);
            }
            leftMouseAdapter.setCommand(cmd);
            leftMenu.addMouseListener(leftMouseAdapter);
            leftMenu.setVisible(true);
        }else if(leftCommandNums==2){
            leftMenu.setText(e.getString("Menu",e.TYPE_OTHER));
            leftMenu.removeMouseListener(leftMouseAdapter);
            BQCommand pCmd=leftMouseAdapter.getCommand();
            JMenuItem jmi=new JMenuItem(pCmd.getLabel());
            jmi.addActionListener(new BQActionListener(displayable,pCmd));
            leftMenu.add(jmi);
            
            jmi=new JMenuItem(cmd.getLabel());
            jmi.addActionListener(new BQActionListener(displayable,cmd));
            leftMenu.add(jmi);
        }else{
            JMenuItem jmi=new JMenuItem(cmd.getLabel());
            jmi.addActionListener(new BQActionListener(displayable,cmd));
            leftMenu.add(jmi);
        }
        Dimension menuSize=leftMenu.getPreferredSize();
        int h=0;
        if(leftCommandNums>1){
            MenuElement[] sub=leftMenu.getPopupMenu().getSubElements();
            if(sub!=null){
                for(int i=0; i<sub.length;i++){
                    h+=((JMenuItem)sub[i]).getPreferredSize().height;
                }
            }
        }
        leftMenu.setMenuLocation(0,-h);
        leftMenu.setBounds(1,1,menuSize.width,BQDisplayable.COMMAND_BAR_HEIGHT-2);
    }
    
    private void buildRightMenu(BQCommand cmd){
        if(rightCommandNums==0){
            rightMenu.setVisible(false);
        }if(rightCommandNums==1){
            rightMenu.setText(cmd.getLabel());
            if(rightMouseAdapter==null){
                rightMouseAdapter=new BQMouseAdapter(displayable,rightMenu);
            }else{
                rightMenu.removeMouseListener(rightMouseAdapter);
            }
            rightMouseAdapter.setCommand(cmd);
            rightMenu.addMouseListener(rightMouseAdapter);
            rightMenu.setVisible(true);
        }else if(rightCommandNums==2){
        	rightMenu.setText(e.getString("Menu",e.TYPE_OTHER));
            rightMenu.removeMouseListener(rightMouseAdapter);
            BQCommand pCmd=rightMouseAdapter.getCommand();
            JMenuItem jmi=new JMenuItem(pCmd.getLabel());
            jmi.addActionListener(new BQActionListener(displayable,pCmd));
            rightMenu.add(jmi);
            
            JMenuItem jmi2=new JMenuItem(cmd.getLabel());
            jmi2.addActionListener(new BQActionListener(displayable,cmd));
            rightMenu.add(jmi2);
        }else{
            JMenuItem jmi=new JMenuItem(cmd.getLabel());
            jmi.addActionListener(new BQActionListener(displayable,cmd));
            rightMenu.add(jmi);
        }
        Dimension menuSize=rightMenu.getPreferredSize();
        int h=0;
        if(rightCommandNums>1){
            MenuElement[] sub=rightMenu.getPopupMenu().getSubElements();
            if(sub!=null){
                for(int i=0; i<sub.length;i++){
                    h+=((JMenuItem)sub[i]).getPreferredSize().height;
                }
            }
        }
        rightMenu.setMenuLocation(0,-h);
        rightMenu.setBounds(displayable.getDisplayWidth()-menuSize.width-1,1,menuSize.width,BQDisplayable.COMMAND_BAR_HEIGHT-2);
    }
    
    public void pressedMenu(boolean left){
        if(left){
            if(leftCommandNums==1){
                leftMouseAdapter.mouseClicked(null);
            }else{
                leftMenu.doClick();
            }
            
        }else{
            if(rightCommandNums==1){
                rightMouseAdapter.mouseClicked(null);
            }else{
                rightMenu.doClick();
            }
        }
    }

    public void resizeScreen(){
    	/*int lw=leftMenu.getPreferredSize().width;
    	int h=0;
        if(leftCommandNums>1){
            MenuElement[] sub=leftMenu.getPopupMenu().getSubElements();
            if(sub!=null){
                for(int i=0; i<sub.length;i++){
                    h+=((JMenuItem)sub[i]).getPreferredSize().height;
                }
            }
        }
        leftMenu.setMenuLocation(0,-h);
    	leftMenu.setBounds(1,1,lw,BQDisplayable.COMMAND_BAR_HEIGHT-2);
    	int rw=rightMenu.getPreferredSize().width;
    	
    	int h2=0;
        if(rightCommandNums>1){
            MenuElement[] sub=rightMenu.getPopupMenu().getSubElements();
            if(sub!=null){
                for(int i=0; i<sub.length;i++){
                    h+=((JMenuItem)sub[i]).getPreferredSize().height;
                }
            }
        }
        rightMenu.setMenuLocation(0,-h2);
    	rightMenu.setBounds(displayable.getDisplayWidth()-rw-1,1,rw,BQDisplayable.COMMAND_BAR_HEIGHT-2);
    	*/
    }
}

⌨️ 快捷键说明

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