简体   繁体   English

如何使 JTextFields 可点击?

[英]How to make JTextFields clickable?

I posted a similar question a year ago, but it was not really well written and I didn't get an answer I could work with.一年前我发布了一个类似的问题,但它写得不是很好,我没有得到可以解决的答案。 Now I stand in front of the same problem.现在我站在同样的问题面前。 I got a JPanel (my content pane), where a MouseListener is implemented.我有一个JPanel (我的内容窗格),其中实现了一个MouseListener

Everywhere I click, I get the exact coordinates of my mouse click.在我点击的任何地方,我都能获得鼠标点击的准确坐标。 Except my JTextField components.除了我的JTextField组件。 When I click on those, the MouseEvent isn't even triggered.当我点击这些时,甚至没有触发MouseEvent H H

ow do I do this, so my mouse clicks on those will also call the mouse event?我该怎么做,所以我的鼠标点击那些也会调用鼠标事件?

Tried: setEnable(false) and setHighlighter(null)尝试: setEnable(false)setHighlighter(null)

Sorry thought I fixed the X/Y problem.抱歉,我以为我解决了 X/Y 问题。

The X/Y problem simply means you are telling us what your attempted solution is without telling us what your requirement is. X/Y 问题只是意味着您告诉我们您尝试的解决方案是什么,而没有告诉我们您的要求是什么。 We can't suggest a different approach if we don't know what you are trying to do.如果我们不知道您要做什么,我们就不能建议不同的方法。

I want to open a menu,我想打开菜单,

Now we know what the requirement is.现在我们知道需求是什么了。

The solution is to add the MouseListener to the text field, not the panel.解决方案是将 MouseListener 添加到文本字段,而不是面板。 If you have the same popup of the panel and the text field, then you still need to add the listener to both the panel and the text field.如果面板和文本字段的弹出窗口相同,那么您仍然需要将侦听器添加到面板和文本字段。

You can do this in one of two ways:您可以通过以下两种方式之一执行此操作:

  1. Read the section from the Swing tutorial on Bringing up a Popup Menu for a working example.阅读 Swing 教程中有关创建弹出菜单的部分,以获取工作示例。

  2. Note the above tutorial is a little old, you can also check out the setComonentPopuMenu(...) method of the JComponent class.注意上面的教程有点老了,你还可以查看JComponent类的setComonentPopuMenu(...)方法。 This approach will create the listener for you.这种方法将为您创建侦听器。

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

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