📄 actionmenubar.java
字号:
}
public void setMenu(ActionMenu menu) {
this.menu = menu;
menuCanvas.sel = -1;
}
public void paint(Graphics ig) {
Dimension s = getSize();
if (backingStore == null
|| (backingStore != null && (s.width != backingStore.getWidth(this) || s.height != backingStore
.getHeight(this)))) {
if (backingStore != null) {
backingStore.getGraphics().dispose();
}
backingStore = createImage(s.width, s.height);
}
Graphics g = backingStore.getGraphics();
g.setColor(getBackground());
g.fillRect(0, 0, s.width - 1, s.height - 1);
int px = 0;
int py = 0;
g.setColor(borderColor);
for (int i = 0; i < borderWidth; i++) {
g.drawRect(px, py, s.width - px - 1, s.height - py - 1);
px++;
py++;
}
if (insets != null) {
px += insets.left;
py += insets.top;
}
if (menu != null) {
Action m = null;
FontMetrics fm = getFontMetrics(getFont());
// py += fm.getHeight() - fm.getDescent();
int i = 0;
for (Enumeration e = menu.children(); e.hasMoreElements();) {
m = (Action) e.nextElement();
if(m.getName().equals(ActionMenu.SEPARATOR.getName())) {
int h = separator.getPreferredSize().height;
int sw = s.width - ( insets == null ? 0 : ( ( insets.left + insets.right ) / 2 ) ) - ( borderWidth * 2 );
separator.setBounds(0, 0, sw, h);
int sx = (s.width - sw ) / 2;
g.translate(sx, py);
separator.paint(g);
g.translate(-sx, -py);
py += h;
}
else {
String n = (String) m.getValue(Action.SHORT_DESCRIPTION);
n = n == null ? m.getName() : n;
if (m.isEnabled()) {
if (i == sel) {
g.setColor(selectionBackground);
int ty = borderWidth + (insets != null ? insets.top : 0) + (py - borderWidth - insets.top );
g.fillRect(borderWidth, ty, s.width - (borderWidth * 2), itemHeight);
if (borderWidth != 0) {
g.setColor(borderColor);
g.drawLine(borderWidth, ty, (borderWidth * 2) + s.width - 1, ty);
g.drawLine(borderWidth, ty + itemHeight - 1, (borderWidth * 2) + s.width - 1, ty + itemHeight - 1);
}
g.setColor(selectionForeground);
} else {
g.setColor(getForeground());
}
} else {
g.setColor(disabledForeground);
}
String imagePath = null;
if (iconType.equals(ActionButton.SMALL_ICONS)) {
imagePath = (String) m.getValue(Action.SMALL_IMAGE_PATH);
} else if (iconType.equals(ActionButton.LARGE_ICONS)) {
imagePath = (String) m.getValue(Action.IMAGE_PATH);
}
int toff = 0;
if (imagePath != null) {
Image img = UIUtil.loadImage(m.getClass(), imagePath);
if(!m.isEnabled()) {
img = createImage(new FilteredImageSource(img.getSource(),
new GrayFilter()));
UIUtil.waitFor(img, this);
}
if (img != null) {
g.drawImage(img, px, py + ((itemHeight - img.getHeight(this)) / 2), this);
toff = imageTextGap + img.getWidth(this);
}
}
g.drawString(n, px + toff, py + fm.getHeight() - fm.getDescent() + ((itemHeight - fm.getHeight()) / 2));
py += itemHeight;
}
i++;
}
}
ig.drawImage(backingStore, 0, 0, this);
}
int getIndexForLocation(int x, int ey) {
int idx = -1;
if (menu != null && getGraphics() != null) {
int y = borderWidth;
if (insets != null) {
y += insets.top;
}
FontMetrics fm = getFontMetrics(getFont());
Action m = null;
for (Enumeration e = menu.children(); e.hasMoreElements();) {
idx++;
m = (Action) e.nextElement();
if(!m.getName().equals(ActionMenu.SEPARATOR.getName())) {
y+= itemHeight;
if(y >= ey) {
return idx;
}
}
else {
y += separator.getPreferredSize().height;
}
}
}
return idx;
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
*/
public void mouseMoved(MouseEvent e) {
sel = getIndexForLocation(e.getX(), e.getY());
if (sel != -1) {
Action action = menu.getChild(sel);
if (action.isEnabled()) {
String tip = (String) action.getValue(Action.LONG_DESCRIPTION);
if (tip != null) {
Point p = getLocationOnScreen();
if (toolTipsEnabled) {
ToolTipManager.getInstance().requestToolTip(this, p.x + e.getX() + 4, p.y + e.getY() + 4, tip);
}
}
} else {
sel = -1;
if (toolTipsEnabled) {
ToolTipManager.getInstance().hide();
}
}
repaint();
}
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
*/
public void mouseClicked(MouseEvent e) {
if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
int clicked = getIndexForLocation(e.getX(), e.getY());
if (clicked != -1 && clicked < menu.getChildCount()) {
Action action = menu.getChild(clicked);
action.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, action.getName(), e.getModifiers()));
}
}
hideMenuWindow();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
*/
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
*/
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
*/
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
*/
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
/*
* (non-Javadoc)
*
* @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
*/
public void focusGained(FocusEvent e) {
}
/*
* (non-Javadoc)
*
* @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
*/
public void focusLost(FocusEvent e) {
hideMenuWindow();
}
/**
*
*/
public void removeAllMenuItems() {
for (Enumeration e = menus.elements(); e.hasMoreElements();) {
MenuAction act = (MenuAction) e.nextElement();
for (Enumeration e2 = act.menu.children(); e2.hasMoreElements();) {
Action a = (Action) e2.nextElement();
a.removePropertyChangeListener(this);
}
}
hideMenuWindow();
menus.removeAllElements();
removeAll();
doLayout();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
*/
public void componentResized(ComponentEvent e) {
hideMenuWindow();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
*/
public void componentMoved(ComponentEvent e) {
hideMenuWindow();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
*/
public void componentShown(ComponentEvent e) {
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
*/
public void componentHidden(ComponentEvent e) {
hideMenuWindow();
}
/*
* (non-Javadoc)
*
* @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {
repaint();
}
/**
* @param background
*/
public void setBaseBackground(Color baseBackground) {
setBackground(baseBackground == null ? SystemColor.control : baseBackground);
this.baseBackground = baseBackground;
for(Enumeration e = menus.elements(); e.hasMoreElements(); ) {
MenuAction m = (MenuAction)e.nextElement();
m.button.setBaseBackground(baseBackground);
}
if(menuCanvas != null) {
menuCanvas.setBaseBackground(baseBackground);
}
}
/**
* @param background
*/
public void setBaseForeground(Color baseForeground) {
setForeground(baseForeground == null ? SystemColor.controlText : baseForeground);
this.baseForeground = baseForeground;
for(Enumeration e = menus.elements(); e.hasMoreElements(); ) {
MenuAction m = (MenuAction)e.nextElement();
m.button.setBaseForeground(baseForeground);
}
if(menuCanvas != null) {
menuCanvas.setBaseForeground(baseForeground);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -