`
xio
  • 浏览: 2254 次
  • 性别: Icon_minigender_1
  • 来自: 永春
文章分类
社区版块
存档分类
最新评论
  • mikewang: 做的,不错, 在给你你加个点击记忆功能 思路 1 每次点击菜单 ...
    JPopupButton

JPopupButton

阅读更多

 

 

本来在网上找了一个JPopupButton【@author SunKing】,但是实际使用的时候,无法满足L&F效果,在JGoodies Looks下无法正常显示。

 

期间还在网络找了一个JPopupToggleButton【@author Alexander Wenckus】,但是感觉过于简单,也不够满意。

 

于是只好自己重写一个。

 

 

以下为相关代码:

 

注意: 勿遗落最后的/name/xio/util/swing/toolbar-buttonarrow.png文件。

 

name.xio.util.swing.JPopupButton

 

package name.xio.util.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.MenuElement;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import name.xio.util.swing.icon.CompositeIcon;
import name.xio.util.swing.icon.HTextIcon;

import com.by.yb.util.Utilities; 
import com.jgoodies.looks.LookUtils;
import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
import com.jgoodies.looks.plastic.theme.ExperienceBlue;
import com.jgoodies.looks.windows.WindowsLookAndFeel;

/**
 * 弹出菜单按钮
 * @author xio
 *
 */
public class JPopupButton extends JButton {

	private static final long serialVersionUID = -6783978058195706887L;

	/**
	 * 弹出菜单
	 */
	private JPopupMenu menu = new JPopupMenu();

	/**
	 * 按钮点击事件,与菜单中的第几项绑定
	 */
	private int attachActionOnPopupMenuIndex = -1;

	/**
	 * 是否显示箭头部分的分割线
	 */
	private boolean showSeparator = false;

	public final static int NARROW_WIDTH = 11;

	public JPopupButton(Icon icon, JPopupMenu menu) {
		this(null, icon, menu);
	}

	public JPopupButton(String text, JPopupMenu menu) {
		this(text, null, menu);
	}

	public JPopupButton(String text, Icon icon, JPopupMenu menu) {
		initView(text, icon, menu);
	}

	/**
	 * 初始化菜单按钮的视图
	 * @param text
	 * @param icon
	 * @param menu
	 */
	private void initView(String text, Icon icon, JPopupMenu menu) {
		//1.按钮绘制

		this.setFocusable(false);
		this.setText(null);

		HTextIcon hTextIcon = new HTextIcon(this, text, 10);
		if (icon != null) {
			CompositeIcon commonButtonIcon = new CompositeIcon(icon, hTextIcon, CompositeIcon.LEFT);
			CompositeIcon popupButtonIcon = new CompositeIcon(commonButtonIcon, new ImageIcon(JPopupButton.class
				.getResource("toolbar-buttonarrow.png")), CompositeIcon.LEFT, 2, 0);

			this.setIcon(popupButtonIcon);
		}
		else {
			CompositeIcon popupButtonIcon = new CompositeIcon(hTextIcon, new ImageIcon(JPopupButton.class
				.getResource("toolbar-buttonarrow.png")), CompositeIcon.LEFT, 2, 0);

			this.setIcon(popupButtonIcon);
		}

		//this.setMargin(new Insets());

		this.menu = menu;

		//2.事件监听

		//Rollover监听
		this.addChangeListener(new RolloverChangeListener());

		//添加按钮鼠标事件监听
		PopupButtonListener popupButtonListener = new PopupButtonListener();
		this.addMouseMotionListener(popupButtonListener);
		this.addMouseListener(popupButtonListener);

		//添加菜单事件监听
		if (menu != null) {
			menu.addPopupMenuListener(popupButtonListener);
		}

		//初始按钮事件绑定
		clearAttachActionOnPopupMenu();

	}

	/**
	 * 清空按钮绑定事件
	 */
	public void clearAttachActionOnPopupMenu() {
		attachActionOnPopupMenu(-1);
	}

	/**
	 * 按钮行为绑定为弹出菜单第一项
	 */
	public void attachDefaultActionOnPopupMenu() {
		attachActionOnPopupMenu(0);
	}

	/**
	 * 按钮的行为绑定为弹出菜单
	 * @param index
	 */
	public void attachActionOnPopupMenu(int index) {
		if (menu == null) {
			attachActionOnPopupMenuIndex = -1;
		}
		else {
			MenuElement[] menuElements = menu.getSubElements();
			if ((index < 0) || (Utilities.isEmpty(menuElements)) || (index >= menuElements.length)) {
				attachActionOnPopupMenuIndex = -1;
			}
			else {
				attachActionOnPopupMenuIndex = index;
			}
		}
	}

	/**
	 * @return Returns the menu.
	 */
	public JPopupMenu getMenu() {
		return menu;
	}

	private void showMenu() {
		if (menu == null) {
			return;
		}
		Point locate = locateMenu();
		menu.show(this, locate.x, locate.y);
	}

	private void showSeparator(boolean b) {
		showSeparator = b;
		this.repaint();
	}

	/**
	 * 按钮左部触发
	 */
	public void onClick() {

		//点击位置为箭头位置  
		boolean mouseInNarrow = isMouseInNarrow();

		if (mouseInNarrow) {
			onClickNarrow();
		}
		else {

			if (attachActionOnPopupMenuIndex < 0) {
				onClickNarrow();
			}
			else {
				MenuElement[] menuElements = menu.getSubElements();
				((AbstractButton) menuElements[attachActionOnPopupMenuIndex]).doClick();
			}
		}
	}

	/**
	 * 按钮箭头部触发
	 */
	private void onClickNarrow() {
		showMenu();
	}

	/**
	 * 菜单位置
	 * @return
	 */
	protected Point locateMenu() {
		Point locate = new Point((int) getAlignmentX(),
			(int) getAlignmentY() + (getHeight() == 0 ? getPreferredSize().height : getHeight()));
		return locate;
	}

	public void paint(Graphics g) {
		super.paint(g);

		//绘制分离线
		if (showSeparator) {
			Color oldColor = g.getColor();

			Insets insets = this.getInsets();

			int width = getWidth();
			int height = getHeight();

			Icon icon = this.getIcon();
			//int x = icon == null ? (width - NARROW_WIDTH) : (mrgin.left - insets.left + icon.getIconWidth() - NARROW_WIDTH);
			int x = icon == null ? (width - NARROW_WIDTH) : (insets.left + icon.getIconWidth() - NARROW_WIDTH);
			int y = 0;

			//			Button.light
			//			Button.shadow
			//			Button.darkShadow
			//			Button.highlight

			//g.setColor(new Color(216, 214, 207));
			g.setColor(UIManager.getColor("Button.shadow"));
			g.drawLine(x, y + 1, x, y + height - 3);

			//g.setColor(new Color(250, 250, 248));
			g.setColor(UIManager.getColor("Button.highlight"));
			g.drawLine(x + 1, y + 1, x + 1, y + height - 3);

			g.setColor(oldColor);
		}
	}

	/**
	 * 当前鼠标是否在按钮上
	 * @return
	 */
	public boolean isMouseOver() {
		return this.getMousePosition() != null;
	}

	/**
	 * 鼠标位置是否在箭头部分
	 * @return
	 */
	public boolean isMouseInNarrow() {
		Point position = this.getMousePosition();
		if (position == null) {
			return false;
		}
		else {
			return isMouseInNarrow(position);
		}
	}

	/**
	 * 鼠标位置是否在箭头部分
	 * @param position
	 * @return
	 */
	public boolean isMouseInNarrow(Point position) {
		Insets insets = this.getInsets();

		Icon icon = this.getIcon();

		int width = this.getWidth();
		int height = this.getHeight();

		//int narrowFromX = icon == null ? (width - NARROW_WIDTH) : (mrgin.left - insets.left + icon.getIconWidth() - NARROW_WIDTH);
		int narrowFromX = icon == null ? (width - NARROW_WIDTH) : (insets.left + icon.getIconWidth() - NARROW_WIDTH);
		int narrowFromY = 0;

		int narrowEndX = width;
		int narrowEndY = height;

		int eventX = (int) position.getX();
		int eventY = (int) position.getY();

		return ((narrowFromX <= eventX) && (eventX <= narrowEndX)) && ((narrowFromY <= eventY) && (eventY <= narrowEndY));
	}

	/**
	 * 按钮Rollover监听
	 * @author xio
	 *
	 */
	class RolloverChangeListener implements ChangeListener {

		public void stateChanged(ChangeEvent e) {
			ButtonModel buttonModel = JPopupButton.this.getModel();
			showSeparator(buttonModel.isRollover() || buttonModel.isPressed());
		}

	}

	/**
	 * 按钮和菜单的事件监听
	 * @author xio
	 *
	 */
	class PopupButtonListener extends MouseAdapter implements PopupMenuListener {

		public void mouseEntered(MouseEvent e) {
			//防止部分L&F未设置Rollover
			JPopupButton.this.getModel().setRollover(true);
		}

		public void mouseExited(MouseEvent e) {
			//防止部分L&F未设置Rollover
			JPopupButton.this.getModel().setRollover(false);
			showSeparator(false);
		}

		public void mouseClicked(MouseEvent e) {
			JPopupButton.this.onClick();
		}

		public void popupMenuCanceled(PopupMenuEvent e) {
		}

		public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
			//菜单不可见时,如果鼠标还在按钮上,设置Rollover状态,以便按钮的视图显示为Rollover
			if (isMouseOver()) {
				JPopupButton.this.getModel().setRollover(true);
			}
		}

		public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
			//菜单显示时,修改按钮Rollover状态
			JPopupButton.this.getModel().setRollover(false);
		}

	}

	public static void main(String[] args) {
		try {
			//不显示ToolBar的边界
			UIManager.put("ToolBar.border", BorderFactory.createEmptyBorder());

			if (LookUtils.IS_LAF_WINDOWS_XP_ENABLED) {
				UIManager.setLookAndFeel(new WindowsLookAndFeel());
			}
			else {
				PlasticXPLookAndFeel.setCurrentTheme(new ExperienceBlue());
				UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
			}

			//								PlasticXPLookAndFeel.setCurrentTheme(new ExperienceBlue());
			//								UIManager.setLookAndFeel(new PlasticXPLookAndFeel());

		}
		catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		JFrame frame = new JFrame("JPopupButton");
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setSize(400, 320);
		//SwingUtils.locateCenterOnScreen(frame);

		class DemoAction extends AbstractAction {

			private static final long serialVersionUID = 5232174589593930544L;

			public DemoAction(String id) {
				super.putValue(Action.NAME, "Demo JPopupButton " + id);
			}

			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog((Component) e.getSource(), super.getValue(Action.NAME));
			}
		}

		JPopupMenu popup = new JPopupMenu("PopupMenu");
		popup.add(new DemoAction("0"));
		popup.add(new DemoAction("1"));
		popup.add(new DemoAction("2"));

		JPopupButton btt1 = new JPopupButton("中文文字", null, popup);

		JPopupButton btt2 = new JPopupButton("中文文字", null, popup);
		btt2.attachDefaultActionOnPopupMenu();

		JPopupButton btt3 = new JPopupButton("中文文字", UIManager.getIcon("FileView.computerIcon"), popup);
		btt3.attachActionOnPopupMenu(2);

		JToolBar toolbar = new JToolBar();

		toolbar.add(btt1);
		toolbar.add(btt2);
		toolbar.add(btt3);

		toolbar.add(new JButton("中文文字", UIManager.getIcon("FileView.computerIcon")));

		toolbar.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 3));

		frame.getContentPane().add(toolbar, BorderLayout.NORTH);
		frame.setVisible(true);
	}

}
 

name.xio.util.swing.icon.CompositeIcon

 

package name.xio.util.swing.icon;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.SwingConstants;

/**
 CompositeIcon is an Icon implementation which draws two icons with a specified relative position:
 LEFT, RIGHT, TOP, BOTTOM specify how icon1 is drawn relative to icon2
 CENTER: icon1 is drawn first, icon2 is drawn over it
 
 and with horizontal and vertical orientations within the alloted space
 
 It's useful with VTextIcon when you want an icon with your text: 
	if icon1 is the graphic icon and icon2 is the VTextIcon, you get a similar effect
	to a JLabel with a graphic icon and text
	
 * @from http://www.macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html?page=last&x-order=date
 * @changedBy xio
 */
public class CompositeIcon implements Icon, SwingConstants {
	Icon fIcon1, fIcon2;

	int fPosition, fHorizontalOrientation, fVerticalOrientation;

	int widthOffset, heightOffset;

	/**
	 Create a CompositeIcon from the specified Icons,
	 using the default relative position (icon1 above icon2)
	 and orientations (centered horizontally and vertically)
	 */
	public CompositeIcon(Icon icon1, Icon icon2) {
		this(icon1, icon2, TOP);
	}

	/**
	Create a CompositeIcon from the specified Icons,
	using the specified relative position
	and default orientations (centered horizontally and vertically)
	*/
	public CompositeIcon(Icon icon1, Icon icon2, int position) {

		this(icon1, icon2, position, CENTER, CENTER, (position == LEFT || position == RIGHT) ? 2 : -7, (position == LEFT || position == RIGHT) ? 0 : 6);
	}

	public CompositeIcon(Icon icon1, Icon icon2, int position, int widthOffset, int heightOffset) {
		this(icon1, icon2, position, CENTER, CENTER, widthOffset, heightOffset);
	}

	/**
	Create a CompositeIcon from the specified Icons,
	using the specified relative position
	and orientations
	*/
	public CompositeIcon(Icon icon1, Icon icon2, int position, int horizontalOrientation, int verticalOrientation, int widthOffset, int heightOffset) {
		fIcon1 = icon1;
		fIcon2 = icon2;
		fPosition = position;
		this.widthOffset = widthOffset;
		this.heightOffset = heightOffset;
		fHorizontalOrientation = horizontalOrientation;
		fVerticalOrientation = verticalOrientation;
	}

	/**
	  * Draw the icon at the specified location.  Icon implementations
	  * may use the Component argument to get properties useful for 
	  * painting, e.g. the foreground or background color.
	  */
	public void paintIcon(Component c, Graphics g, int x, int y) {
		int width = getIconWidth();
		int height = getIconHeight();
		if (fPosition == LEFT || fPosition == RIGHT) {
			Icon leftIcon, rightIcon;
			if (fPosition == LEFT) {
				leftIcon = fIcon1;
				rightIcon = fIcon2;
			}
			else {
				leftIcon = fIcon2;
				rightIcon = fIcon1;
			}
			// "Left" orientation, because we specify the x position
			paintIcon(c, g, leftIcon, x, y, width, height, LEFT, fVerticalOrientation);
			paintIcon(c, g, rightIcon, x + leftIcon.getIconWidth(), y, width, height, LEFT, fVerticalOrientation);
		}
		else if (fPosition == TOP || fPosition == BOTTOM) {
			Icon topIcon, bottomIcon;
			if (fPosition == TOP) {
				topIcon = fIcon1;
				bottomIcon = fIcon2;
			}
			else {
				topIcon = fIcon2;
				bottomIcon = fIcon1;
			}
			// "Top" orientation, because we specify the y position
			paintIcon(c, g, topIcon, x, y, width, height, fHorizontalOrientation, TOP);
			paintIcon(c, g, bottomIcon, x, y + topIcon.getIconHeight(), width, height, fHorizontalOrientation, TOP);
		}
		else {
			paintIcon(c, g, fIcon1, x, y, width, height, fHorizontalOrientation, fVerticalOrientation);
			paintIcon(c, g, fIcon2, x, y, width, height, fHorizontalOrientation, fVerticalOrientation);
		}
	}

	/* Paints one icon in the specified rectangle with the given orientations
	*/
	void paintIcon(Component c, Graphics g, Icon icon, int x, int y, int width, int height, int horizontalOrientation,
		int verticalOrientation) {

		int xIcon, yIcon;
		switch (horizontalOrientation) {
			case LEFT:
				xIcon = x;
				break;
			case RIGHT:
				xIcon = x + width - icon.getIconWidth();
				break;
			default:
				xIcon = x + (width - icon.getIconWidth()) / 2;
				break;
		}
		switch (verticalOrientation) {
			case TOP:
				yIcon = y;
				break;
			case BOTTOM:
				yIcon = y + height - icon.getIconHeight();
				break;
			default:
				yIcon = y + (height - icon.getIconHeight()) / 2;
				break;
		}
		icon.paintIcon(c, g, xIcon, yIcon);
	}

	/**
	 * Returns the icon's width.
	 *
	 * @return an int specifying the fixed width of the icon.
	 */
	public int getIconWidth() {
		if (fPosition == LEFT || fPosition == RIGHT)
			return fIcon1.getIconWidth() + fIcon2.getIconWidth() + widthOffset;

		return Math.max(fIcon1.getIconWidth(), fIcon2.getIconWidth()) + widthOffset;
	}

	/**
	 * Returns the icon's height.
	 *
	 * @return an int specifying the fixed height of the icon.
	 */
	public int getIconHeight() {
		if (fPosition == TOP || fPosition == BOTTOM)
			return fIcon1.getIconHeight() + fIcon2.getIconHeight() + heightOffset;

		return Math.max(fIcon1.getIconHeight(), fIcon2.getIconHeight()) + heightOffset;
	}

}
 

name.xio.util.swing.icon.HTextIcon

 

package name.xio.util.swing.icon;

import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Icon;

/**
 * 水平文本Icon

 * @author xio
 *
 */
public class HTextIcon implements Icon, PropertyChangeListener {
	private String text;

	private int textWidth;

	private int textHeight;

	private int fontCharHeight;

	private Component fontComponent;

	private int marginLeft;

	private int marginRight;

	static final int MARGIN = 4;

	static final int MIN_TEXT_HEIGHT = 15;

	static final int MIN_TEXT_WIDTH = 16;

	public HTextIcon(Component component, String label) {
		this(component, label, MARGIN, MARGIN);
	}

	public HTextIcon(Component component, String label, int marginRight) {
		this(component, label, MARGIN, marginRight);
	}

	/**
	 * 指定一个Component,创建水平文本Icon
	 * @param component
	 * @param label
	 */
	public HTextIcon(Component component, String label, int marginLeft, int marginRight) {
		fontComponent = component;
		text = label;
		this.marginLeft = marginLeft;
		this.marginRight = marginRight;
		calcDimensions();
		fontComponent.addPropertyChangeListener(this);
	}

	/**
	 * sets the label to the given string, updating the orientation as needed
	 * and invalidating the layout if the size changes
	 * @see #verifyRotation
	 */
	public void setLabel(String label) {
		text = label;
		recalcDimensions();
	}

	/**
	 * Checks for changes to the font on the fComponent
	 * so that it can invalidate the layout if the size changes
	 */
	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();
		if ("font".equals(prop)) {
			recalcDimensions();
		}
	}

	/** 
	 * Calculates the dimensions.  If they've changed,
	 * invalidates the component
	 */
	void recalcDimensions() {
		int wOld = getIconWidth();
		int hOld = getIconHeight();
		calcDimensions();
		if (wOld != getIconWidth() || hOld != getIconHeight())
			fontComponent.invalidate();
	}

	void calcDimensions() {
		FontMetrics fontMetrics = fontComponent.getFontMetrics(fontComponent.getFont());
		int fontDescent = fontMetrics.getDescent();
		fontCharHeight = fontMetrics.getAscent() + fontDescent;

		textWidth = fontMetrics.stringWidth(text) + marginLeft + marginRight;
		if (textWidth < MIN_TEXT_WIDTH) {
			textWidth = MIN_TEXT_WIDTH;
		}

		textHeight = fontCharHeight + fontDescent;
		if (textHeight < MIN_TEXT_HEIGHT) {
			textHeight = MIN_TEXT_HEIGHT;
		}

		//		//强制调整尺寸
		//		fWidth -= 5;
		//		fHeight += 6;

	}

	/**
	 * 绘制文本
	 */
	public void paintIcon(Component c, Graphics g, int x, int y) {
		g.setColor(c.getForeground());
		g.setFont(c.getFont());
		g.drawString(text, x + marginLeft, y + fontCharHeight - 2);

	}

	/**
	 * icon宽度
	 */
	public int getIconWidth() {
		return textWidth;
	}

	/**
	 * icon高度
	 */
	public int getIconHeight() {
		return textHeight;
	}

}
 

 

 

/name/xio/util/swing/toolbar-buttonarrow.png: http://xio.iteye.com/upload/attachment/44097/f0b31625-ac6e-36a5-a5cd-d86bca96e72d.png

 

 

 

  • 大小: 2 KB
  • 大小: 20 KB
分享到:
评论
1 楼 mikewang 2009-04-09  
做的,不错, 在给你你加个点击记忆功能
思路
1 每次点击菜单的时候保存这个菜单的index,并且出发菜单下表变更事件
2 在JPopupButton中 监听事件, 并调用attachActionOnPopupMenu 绑定点击按钮所执行的菜单

实现:
JPopupMenu 达不到要求, 需要改造一下
package info.mikewang.part.test;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;

class JPopupButtonMenu extends JPopupMenu {
	private static final long serialVersionUID = -8812143869773395718L;

	private int clickedMenuIndex = -1;

	public JPopupButtonMenu() {
		this(null);
	}

	public JPopupButtonMenu(String label) {
		super(label);
		addContainerListener(new XContainerListener());
	}

	public int getClickedMenuIndex() {
		return clickedMenuIndex;
	}

	public AbstractButton[] getSubClickedElements() {
		AbstractButton result[];
		Vector<AbstractButton> tmpx = new Vector<AbstractButton>();

		MenuElement tmp[] = getSubElements();
		for (MenuElement menuElement : tmp) {
			if (menuElement instanceof AbstractButton) {
				tmpx.add((AbstractButton) menuElement);
			}
		}

		result = new AbstractButton[tmpx.size()];
		for (int i = 0; i < tmpx.size(); i++) {
			result[i] = tmpx.elementAt(i);
		}

		return result;
	}

	// ----------------------------------

	class XContainerListener implements ContainerListener {

		@Override
		public void componentAdded(ContainerEvent e) {
			Component c = e.getChild();
			if (c instanceof AbstractButton) {
				((AbstractButton) c).addActionListener(new XActionListener());
			}
		}

		@Override
		public void componentRemoved(ContainerEvent e) {

		}
	}

	class XActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			AbstractButton ab = (AbstractButton) e.getSource();
			AbstractButton abs[] = getSubClickedElements();
			for (int i = 0; i < abs.length; i++) {
				if (ab == abs[i]) {
					int oldClickedMenuIndex = clickedMenuIndex;
					clickedMenuIndex = i;
					firePropertyChange("clickedMenuIndex", oldClickedMenuIndex, i);
					break;
				}
			}
		}
	}
}


JPopupButton 本部分都差不多, 就增加了一个属性监听器, 监听clickedMenuIndex, 并调用attachActionOnPopupMenu, 改变下标
private void initClickRemember() {
		menu.addPropertyChangeListener("clickedMenuIndex",
				new PropertyChangeListener() {
					@Override
					public void propertyChange(PropertyChangeEvent evt) {
						Integer ii = (Integer) evt.getNewValue();
						attachActionOnPopupMenu(ii.intValue());
					}
				});
	}

相关推荐

    OpenSwing---Java 常用控件集合

    2006/01/20 修正了JPopupButton在XP风格下呈两个按钮样子的BUG 对JFontDialog进行了部分修改 2006/03/08 从JDatePicker提出来一个组件JCalendarPanel日期选择面板 并修改了JDatePicker的设计方式 2006/03/23 ...

    OpenSwing开发包及源码

    只能输入0~255数字的IP地址 JListChooser 列表选择对话框 JNumberField 数字输入框,可限制小数位数,数字最大长度,最大最小能输入的数字 JPopupButton 带下拉菜单的工具栏按钮 JStatusBar 模拟Windows的...

    级联H桥SVG无功补偿系统在不平衡电网中的三层控制策略:电压电流双闭环PI控制、相间与相内电压均衡管理,级联H桥SVG无功补偿系统在不平衡电网中的三层控制策略:电压电流双闭环PI控制、相间与相内电压均

    级联H桥SVG无功补偿系统在不平衡电网中的三层控制策略:电压电流双闭环PI控制、相间与相内电压均衡管理,级联H桥SVG无功补偿系统在不平衡电网中的三层控制策略:电压电流双闭环PI控制、相间与相内电压均衡管理,不平衡电网下的svg无功补偿,级联H桥svg无功补偿statcom,采用三层控制策略。 (1)第一层采用电压电流双闭环pi控制,电压电流正负序分离,电压外环通过产生基波正序有功电流三相所有H桥模块直流侧平均电压恒定,电流内环采用前馈解耦控制; (2)第二层相间电压均衡控制,注入零序电压,控制通过注入零序电压维持相间电压平衡; (3)第三层相内电压均衡控制,使其所有子模块吸收的有功功率与其损耗补,从而保证所有H桥子模块直流侧电压值等于给定值。 有参考资料。 639,核心关键词: 1. 不平衡电网下的SVG无功补偿 2. 级联H桥SVG无功补偿STATCOM 3. 三层控制策略 4. 电压电流双闭环PI控制 5. 电压电流正负序分离 6. 直流侧平均电压恒定 7. 前馈解耦控制 8. 相间电压均衡控制 9. 零序电压注入 10. 相内电压均衡控制 以上十个关键词用分号分隔的格式为:不

    GTX 1080 PCB图纸

    GTX 1080 PCB图纸,内含图纸查看软件

    深度优化与应用:提升DeepSeek润色指令的有效性和灵活性指南

    内容概要:本文档详细介绍了利用 DeepSeek 进行文本润色和问答交互时提高效果的方法和技巧,涵盖了从明确需求、提供适当上下文到尝试开放式问题以及多轮对话的十个要点。每一部分内容都提供了具体的示范案例,如指定回答格式、分步骤提问等具体实例,旨在指导用户更好地理解和运用 DeepSeek 提升工作效率和交流质量。同时文中还强调了根据不同应用场景调整提示词语气和风格的重要性和方法。 适用人群:适用于希望通过优化提问技巧以获得高质量反馈的企业员工、科研人员以及一般公众。 使用场景及目标:本文针对所有期望提高 DeepSeek 使用效率的人群,帮助他们在日常工作中快速获取精准的答案或信息,特别是在撰写报告、研究材料准备和技术咨询等方面。此外还鼓励用户通过不断尝试不同形式的问题表述来进行有效沟通。 其他说明:该文档不仅关注实际操作指引,同样重视用户思维模式转变——由简单索取答案向引导 AI 辅助创造性解决问题的方向发展。

    基于FPGA与W5500实现的TCP网络通信测试平台开发-Zynq扩展口Verilog编程实践,基于FPGA与W5500芯片的TCP网络通信测试及多路Socket实现基于zynq开发平台和Vivad

    基于FPGA与W5500实现的TCP网络通信测试平台开发——Zynq扩展口Verilog编程实践,基于FPGA与W5500芯片的TCP网络通信测试及多路Socket实现基于zynq开发平台和Vivado 2019软件的扩展开发,基于FPGA和W5500的TCP网络通信 测试平台 zynq扩展口开发 软件平台 vivado2019.2,纯Verilog可移植 测试环境 压力测试 cmd命令下ping电脑ip,同时采用上位机进行10ms发包回环测试,不丢包(内部数据回环,需要时间处理) 目前实现单socket功能,多路可支持 ,基于FPGA; W5500; TCP网络通信; Zynq扩展口开发; 纯Verilog可移植; 测试平台; 压力测试; 10ms发包回环测试; 单socket功能; 多路支持。,基于FPGA与W5500的Zynq扩展口TCP通信测试:可移植Verilog实现的高效网络通信

    Labview液压比例阀伺服阀试验台多功能程序:PLC通讯、液压动画模拟、手动控制与调试、传感器标定、报警及记录、自动实验、数据处理与查询存储,报表生成与打印一体化解决方案 ,Labview液压比例阀

    Labview液压比例阀伺服阀试验台多功能程序:PLC通讯、液压动画模拟、手动控制与调试、传感器标定、报警及记录、自动实验、数据处理与查询存储,报表生成与打印一体化解决方案。,Labview液压比例阀伺服阀试验台多功能程序:PLC通讯、液压动画模拟、手动控制与调试、传感器标定、报警管理及实验自动化,labview液压比例阀伺服阀试验台程序:功能包括,同PLC通讯程序,液压动画,手动控制及调试,传感器标定,报警设置及报警记录,自动实验,数据处理曲线处理,数据库存储及查询,报表自动生成及打印,扫码枪扫码及信号录入等~ ,核心关键词:PLC通讯; 液压动画; 手动控制及调试; 传感器标定; 报警设置及记录; 自动实验; 数据处理及曲线处理; 数据库存储及查询; 报表生成及打印; 扫码枪扫码。,Labview驱动的智能液压阀测试系统:多功能控制与数据处理

    华为、腾讯、万科员工职业发展体系建设与实践.pptx

    华为、腾讯、万科员工职业发展体系建设与实践.pptx

    基于遗传算法的柔性车间调度优化 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    电网不对称故障下VSG峰值电流限制的柔性控制策略:实现电流平衡与功率容量的优化利用,电网不对称故障下VSG峰值电流限制的柔性控制策略:兼顾平衡电流与功率控制切换的动态管理,电网不对称故障下VSG峰值电

    电网不对称故障下VSG峰值电流限制的柔性控制策略:实现电流平衡与功率容量的优化利用,电网不对称故障下VSG峰值电流限制的柔性控制策略:兼顾平衡电流与功率控制切换的动态管理,电网不对称故障下VSG峰值电流限制的柔性不平衡控制(文章完全复现)。 提出一种在不平衡运行条件下具有峰值电流限制的可变不平衡电流控制方法,可灵活地满足不同操作需求,包括电流平衡、有功或无功恒定运行(即电流控制、有功控制或无功控制之间的相互切),注入电流保持在安全值内,以更好的利用VSG功率容量。 关键词:VSG、平衡电流控制、有功功率控制、无功功率控制。 ,VSG; 峰值电流限制; 柔性不平衡控制; 电流平衡控制; 有功功率控制; 无功功率控制。,VSG柔性控制:在电网不对称故障下的峰值电流限制与平衡管理

    libpinyin-tools-0.9.93-4.el7.x64-86.rpm.tar.gz

    1、文件内容:libpinyin-tools-0.9.93-4.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/libpinyin-tools-0.9.93-4.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊

    机器学习(预测模型):动漫《龙珠》相关的数据集

    数据集是一个以经典动漫《龙珠》为主题的多维度数据集,广泛应用于数据分析、机器学习和图像识别等领域。该数据集由多个来源整合而成,涵盖了角色信息、战斗力、剧情片段、台词以及角色图像等多个方面。数据集的核心内容包括: 角色信息:包含《龙珠》系列中的主要角色及其属性,如名称、种族、所属系列(如《龙珠》《龙珠Z》《龙珠超》等)、战斗力等级等。 图像数据:提供角色的图像资源,可用于图像分类和角色识别任务。这些图像来自动画剧集、漫画和相关衍生作品。 剧情与台词:部分数据集还包含角色在不同故事中的台词和剧情片段,可用于文本分析和自然语言处理任务。 战斗数据:记录角色在不同剧情中的战斗力变化和战斗历史,为研究角色成长和剧情发展提供支持。 数据集特点 多样性:数据集整合了角色、图像、文本等多种类型的数据,适用于多种研究场景。 深度:不仅包含角色的基本信息,还涵盖了角色的成长历程、技能描述和与其他角色的互动关系。 实用性:支持多种编程语言(如Python、R)的数据处理和分析,提供了详细的文档和示例代码。

    基于protues仿真的多功公交站播报系统设计(仿真图、源代码)

    基于protues仿真的多功公交站播报系统设计(仿真图、源代码) 该设计为基于protues仿真的多功公交站播报系统,实现温度显示、时间显示、和系统公交站播报功能; 具体功能如下: 1、系统使用51单片机为核心设计; 2、时钟芯片进行时间和日期显示; 3、温度传感器进行温度读取; 4、LCD12864液晶屏进行相关显示; 5、按键设置调节时间; 6、按键设置报站; 7、仿真图、源代码; 操作说明: 1、下行控制报站:首先按下(下行设置按键),(下行指示灯)亮,然后按下(手动播报)按键控制播报下一站; 2、上行控制报站:首先按上(上行设置按键),(上行指示灯)亮,然后按下(手动播报)按键控制播报下一站; 3、按下关闭播报按键,则关闭播报功能和清除显示

    基于微信小程序的琴房管理系统的设计与实现.zip

    采用Java后台技术和MySQL数据库,在前台界面为提升用户体验,使用Jquery、Ajax、CSS等技术进行布局。 系统包括两类用户:学生、管理员。 学生用户 学生用户只要实现了前台信息的查看,打开首页,查看网站介绍、琴房信息、在线留言、轮播图信息公告等,通过点击首页的菜单跳转到对应的功能页面菜单,包括网站首页、琴房信息、注册登录、个人中心、后台登录。 学生用户通过账户账号登录,登录后具有所有的操作权限,如果没有登录,不能在线预约。学生用户退出系统将注销个人的登录信息。 管理员通过后台的登录页面,选择管理员权限后进行登录,管理员的权限包括轮播公告管理、老师学生信息管理和信息审核管理,管理员管理后点击退出,注销登录信息。 管理员用户具有在线交流的管理,琴房信息管理、琴房预约管理。 在线交流是对前台用户留言内容进行管理,删除留言信息,查看留言信息。

    界面GUI设计MATLAB教室人数统计.zip

    MATLAB可以用于开发人脸识别考勤系统。下面是一个简单的示例流程: 1. 数据采集:首先收集员工的人脸图像作为训练数据集。可以要求员工提供多张照片以获得更好的训练效果。 2. 图像预处理:使用MATLAB的图像处理工具对采集到的人脸图像进行预处理,例如灰度化、裁剪、缩放等操作。 3. 特征提取:利用MATLAB的人脸识别工具包,如Face Recognition Toolbox,对处理后的图像提取人脸特征,常用的方法包括主成分分析(PCA)和线性判别分析(LDA)等。 4. 训练模型:使用已提取的人脸特征数据集训练人脸识别模型,可以选择支持向量机(SVM)、卷积神经网络(CNN)等算法。 5. 考勤系统:在员工打卡时,将摄像头捕获的人脸图像输入到训练好的模型中进行识别,匹配员工信息并记录考勤数据。 6. 结果反馈:根据识别结果,可以自动生成考勤报表或者实时显示员工打卡情况。 以上只是一个简单的步骤,实际开发过程中需根据具体需求和系统规模进行定制和优化。MATLAB提供了丰富的图像处理和机器学习工具,是开发人脸识别考勤系统的一个很好选择。

    hjbvbnvhjhjg

    hjbvbnvhjhjg

    HCIP、软考相关学习PPT

    HCIP、软考相关学习PPT提供下载

    绿豆BOX UI8版:反编译版六个全新UI+最新后台直播管理源码

    绿豆BOX UI8版:反编译版六个全新UI+最新后台直播管理源码 最新绿豆BOX反编译版六个UI全新绿豆盒子UI8版本 最新后台支持直播管理 作为UI6的升级版,UI8不仅修复了前一版本中存在的一些BUG,还提供了6套不同的UI界面供用户选择,该版本有以下特色功能: 在线管理TVBOX解析 在线自定义TVBOX 首页布局批量添加会员信息 并支持导出批量生成卡密 并支持导出直播列表管理功能

    vue3的一些语法以及知识点

    vue3的一些语法以及知识点

    西门子大型Fanuc机器人汽车焊装自动生产线程序经典解析:PLC博图编程与MES系统通讯实战指南,西门子PLC博图汽车焊装自动生产线FANUC机器人程序经典结构解析与MES系统通讯,西门子1500 大

    西门子大型Fanuc机器人汽车焊装自动生产线程序经典解析:PLC博图编程与MES系统通讯实战指南,西门子PLC博图汽车焊装自动生产线FANUC机器人程序经典结构解析与MES系统通讯,西门子1500 大型程序fanuc 机器人汽车焊装自动生产线程序 MES 系统通讯 大型程序fanuc机器人汽车焊装自动生产线程序程序经典结构清晰,SCL算法堆栈,梯形图和 SCL混编使用博图 V14以上版本打开 包括: 1、 PLC 博图程序 2 触摸屏程序 ,西门子1500; 大型程序; fanuc机器人; 汽车焊装自动生产线; MES系统通讯; SCL算法; 梯形图; SCL混编; 博图V14以上版本。,西门子博图大型程序:汽车焊装自动生产线MES系统通讯与机器人控制

Global site tag (gtag.js) - Google Analytics