一个 jPopup 用于多个控件
我有这样的代码:
jTextArea1.add(jPopupMenu1);
jTextArea1.setComponentPopupMenu(jPopupMenu1);
jTextField1.add(jPopupMenu2);
jTextField1.setComponentPopupMenu(jPopupMenu2);
对于菜单项,我有操作:
private void CopyActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.copy();
}
private void Copy1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.copy();
}
现在我认为最好对所有文本组件使用一个弹出窗口,如何传递有关单击哪个组件来复制文本的信息?也许对于这种情况有一些更通用的解决方案?
I have code like this:
jTextArea1.add(jPopupMenu1);
jTextArea1.setComponentPopupMenu(jPopupMenu1);
jTextField1.add(jPopupMenu2);
jTextField1.setComponentPopupMenu(jPopupMenu2);
and for menu items I have actions:
private void CopyActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.copy();
}
private void Copy1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.copy();
}
Now I think it would be better to use one popup for all text components, how to pass info about which component was clicked to copy text? Maybe there is some more general solution for such case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动作应该通过扩展 TextAction 来创建。 TextAction 类有一个方法将返回最后获得焦点的文本组件。然后可以在弹出菜单或添加到菜单栏的菜单上使用此操作。因此,创建菜单项的基本代码是:
但是,它比这更容易,因为 DefaultEditorKit 已经提供了默认的复制操作,因此您需要做的就是:
Actions should be created by extending TextAction. The TextAction class has a method that will return the text component that last has focus. This action can then be used on a popup menu or on a menu added to the menu bar. So the basic code to create the menu item would be:
However, its even easier than that because the DefaultEditorKit already provides a default copy action so all you need to do is:
Event 类有一个 getSource() 方法,可以告诉您哪个组件是事件的原因。
the Event class has a getSource() method that tells you what component was the cause of the event.