你可以自己定义一个 MyMenuItem 从 JMenuItem 继承,给它一个 Object 属性,构造的时候比构造 JMenuItem 多一个 Object 方法。假设有一个构造定义为 MyMenuItem(String s, Object obj)
然后你就可以使用 popup.add(new MyMenuItem("string", ojb)) 来达到你的目的了
稍后给你个示例代码
然后你就可以使用 popup.add(new MyMenuItem("string", ojb)) 来达到你的目的了
稍后给你个示例代码
* @(#) TestFrame.java
* Created on 2004-9-25
* By James Fancy
*/import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;/**
* TestFrame
*
* @author James
*/
public class TestFrame extends JFrame { private JButton button; public static void main(String args[]) {
TestFrame frame = new TestFrame();
frame.setBounds(100, 100, 225, 138);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} public static class MyMenuItem extends JMenuItem { Object obj; public MyMenuItem(String s, Object o) {
super(s);
obj = o;
}
} public TestFrame() {
super(); button = new JButton();
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
JPopupMenu pm = new JPopupMenu();
pm.add(new JMenuItem("hello"));
JMenuItem item = pm.add(new MyMenuItem("test", this));
item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
MyMenuItem item = (MyMenuItem) e.getSource();
System.out.println(item.obj.getClass().getName());
}
});
pm.show(button, 0, 0);
}
});
getContentPane().add(button, BorderLayout.NORTH);
button.setText("Test PopupMenu");
}
}
item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
MyMenuItem item = (MyMenuItem) e.getSource();
System.out.println(item.obj.getClass().getName());
}
});
JPopupMenu 似乎不能吧,有时间我查查
t是我在
ActionTask t=new ActionTask();
JMenuItem item = pm.add(new MyMenuItem("test", this));
item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
myopen(t);//
MyMenuItem item = (MyMenuItem) e.getSource();
System.out.println(item.obj.getClass().getName());
}
});
的外部定义的一个类,请问出现下面的错误提示我该怎么解决呀
"SysLogWin.java": local variable t is accessed from within inner class; needs to be declared final at line 230, column 32