//MenuBarLabel 用 于 模 拟java.awt.MenuBar 的 功 能 public class MenuBarLabel extends Canvas implements MouseListener { protected Color Foreground = Color.black ; protected Color Background = Color.lightGray; protected String xlabel ; protected boolean disabled = false ; protected MenuBarLabelListener listener=null;
public MenuBarLabel(String xlabel) { this.xlabel = new String(xlabel); setFont(new Font("Courier",Font.PLAIN,16)); setBackground(Background); addMouseListener(this); }
public void addListener(MenuBarLabelListener listener) { this.listener=listener; }
public String getName() { return xlabel; }
public void paint(Graphics g) { Dimension dm = getSize(); int width = dm.width; int height= dm.height; g.setColor(Foreground);
int descent = g.getFontMetrics().getDescent(); g.drawString(xlabel,0,height-descent); } public void mouseClicked(MouseEvent evt){ ;} public void mouseEntered(MouseEvent evt){ ;} public void mousePressed(MouseEvent evt) { Background=Color.blue.darker().darker(); paint(); if (null !=listener) listener.ProcessLabelClick(xlabel); }
public void mouseExited(MouseEvent evt) { Background=Color.lightGray; setBackground(Background); }
public void mouseReleased(MouseEvent evt) { Background=Color.lightGray; setBackground(Background); }
public void setForeground(Color Foreground) { this.Foreground = Foreground; repaint(); }
public void setBackground(Color Background) { this.Background = Background; repaint(); }
public void paint() { Graphics g=getGraphics(); if (null==g) return; Dimension dm = getSize(); int width = dm.width; int height= dm.height; int descent = g.getFontMetrics().getDescent(); int height1=g.getFontMetrics().getHeight(); g.setColor(Color.blue.darker().darker()); g.fillRect(0,height-height1,width,height1); g.setColor(Color.white); g.drawString(xlabel,0,height-descent); g.dispose(); }
public void paintBack() { Graphics g=getGraphics(); if (null==g) return; Dimension dm = getSize(); int width = dm.width; int height= dm.height; int descent = g.getFontMetrics().getDescent(); g.setColor(Color.lightGray); g.fillRect(0,0,width,height); g.setColor(Foreground); g.drawString(xlabel,0,height-descent); g.dispose(); } }
// 此listener 用 于 提 供 所 有 的 菜 单 选 择 事 件 public interface MenuPanelListener { public void ProcessMyMenu(Object source,Object label); //you can use source to indentify different items with same label }
//MenuPanel 提 供 菜 单 功 能 服 务 import java.awt.*; import java.awt.event.*; import java.util.*; /** *Pay attention: *1.The MenuPanel′s height >==26, 26 recommended *2.You can not change the background of the menu because Popup * menu′s backgroud can not be changed */
public class MenuPanel extends Panel implements ActionListener,MenuBarLabelListener
public void addListener(MenuPanelListener listener) { this.listener=listener; }
private void addMenuItemListener(MenuComponent menu) { ((MenuItem)menu).addActionListener(this); if (menu instanceof Menu menu instanceof PopupMenu) for (int i=0;i< ((Menu)menu).getItemCount();i++) addMenuItemListener(((Menu)menu).getItem(i));
}
private void setMenuItemFont(MenuComponent menu) { menu.setFont(labelFont); if (menu instanceof Menu menu instanceof PopupMenu) for (int i=0;i< ((Menu)menu).getItemCount();i++) setMenuItemFont(((Menu)menu).getItem(i)); }