简体   繁体   English

如何向 JMenuItem 添加标签?

[英]How do I add a tag to JMenuItem?

How can I set a tag for my menu item so that I can ue it later in the callback?如何为我的菜单项设置标签,以便稍后在回调中使用它?

Something like this.像这样的东西。 Somebody have ever do it?有人做过吗?

JMenuItem item = new JMenuItem(mnu.text);
item.setSomething(myTag) ???;
                    
item.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(java.awt.event.ActionEvent evt) 
   {
      start_something(myTag);
   }
});

You can use .setName() method for tagging it您可以使用.setName()方法对其进行标记

    final JMenuItem item = new JMenuItem();
    item.setName("item1");

    item.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String tag = item.getName();
        }
    });

You can create a subclass as mentioned by Adir D but you can also add properties to the component itself and read those properties somewhere else.您可以创建 Adir D 提到的子类,但您也可以向组件本身添加属性并在其他地方读取这些属性。 For a small number of properties or where a subclass doesn't fit, it might solve your problem.对于少数属性或子类不适合的情况,它可能会解决您的问题。

See https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html请参阅https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html

putClientProperty放置客户端属性

public final void putClientProperty(Object key, Object value) public final void putClientProperty(Object key, Object value)

Adds an arbitrary key/value "client property" to this component.向该组件添加任意键/值“客户端属性”。

The get/putClientProperty methods provide access to a small per-instance hashtable. get/putClientProperty 方法提供对每个实例的小型哈希表的访问。 Callers can use get/putClientProperty to annotate components that were created by another module.调用者可以使用 get/putClientProperty 来注释由另一个模块创建的组件。 For example, a layout manager might store per child constraints this way.例如,布局管理器可能会以这种方式存储每个子约束。 For example:例如:

 componentA.putClientProperty("to the left of", componentB);

If value is null this method will remove the property.如果值为 null,则此方法将删除该属性。 Changes to client properties are reported with PropertyChange events.通过 PropertyChange 事件报告对客户端属性的更改。 The name of the property (for the sake of PropertyChange events) is key.toString().属性的名称(为了 PropertyChange 事件)是 key.toString()。

The clientProperty dictionary is not intended to support large scale extensions to JComponent nor should be it considered an alternative to subclassing when designing a new component. clientProperty 字典不打算支持对 JComponent 的大规模扩展,也不应该在设计新组件时将其视为子类化的替代方法。

Parameters:参数:

key - the new client property key key - 新的客户端属性键

value - the new client property value; value - 新的客户端属性值; if null this method will remove the property如果为 null 此方法将删除该属性

See Also: getClientProperty(java.lang.Object), Container.addPropertyChangeListener(java.beans.PropertyChangeListener)另见:getClientProperty(java.lang.Object), Container.addPropertyChangeListener(java.beans.PropertyChangeListener)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM