单击在 Java 中添加类似动态文本区域的对象
标题有点令人困惑,但我将使用 Java 和 Jframe。基本上,我希望能够单击表单上的任意位置并显示“文本区域/框”(也许使用 JTextField 或 JTextArea ?)。我希望用户也能够编辑、删除和移动该字符串。
我想我需要一个动作监听器来监听表单上的点击。每次单击都会调用创建一个新的文本“框”。我不知道如何使这个“框”可由用户编辑、删除或移动。
我也需要一种存储字符串和坐标数据的方法。简单地扩展 JTextField 或 JTextArea 来向它们添加坐标信息是个好主意吗?我看到 swing 是基于事件的,所以我需要某种触发器来“保存”文本(正在考虑回车键,但我意识到我希望用户能够输入多行字符串)。
任何想法将不胜感激。我熟悉 Java,但对 UI 部分只有一点经验。
The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要一个
MouseListener
来代替ActionListener
来跟踪点击。听起来您需要一个未装饰的
JInternalFrame
,其中在JDesktopPane
上有一个文本框。但是,我不认为您可以创建一个未修饰的 JInternalFrame,也许可以从一个带有 TextBox 的普通JInternalFrame
开始,然后通过在桌面窗格上单击鼠标来创建新框架。然后看看是否可以使JInternalFrame
更像一个Window
。另一种途径是自定义组件,它可以完成您需要的一切。这是可能的,只是需要更多的自定义代码。
Instead of an
ActionListener
you will need aMouseListener
to track clicks.Sounds like you need an undecorated
JInternalFrame
with a text box in it onJDesktopPane
. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normalJInternalFrame
with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make theJInternalFrame
more like aWindow
.Another route is a custom component that does everything you need. This is possible, just a lot more custom code.