自定义 JTextField
我想知道如何自定义 jtextfield 的 ui,以便我可以创建圆角矩形边框,而文档不会超出边框。
到目前为止,我想我已经尝试了我能想到的大部分方法,我创建了一个新的 FieldView 类,并根据我绘制圆角矩形的自定义边框更改了绘制方法中的形状,这是我设法获得的唯一方法摆脱白色文本字段文档/视图的方法是将其设置为不透明,但我认为应该有另一种方法而不设置不透明值。
您有自定义 jtextfield 的 laf 的经验吗?请回信,我什至读过 Core Swing 高级书籍,但没有运气,如果您尝试使用 google 搜索,请告诉我搜索短语,因为我尝试过使用“样式”等关键字、“定制”、“ui”、“plaf”、“laf”等等。
我真诚地希望你能给我一个正确的方向,我希望没有人会因此而引起争论,我已经真正使用了我能想到的所有资源。
诚挚的问候。
I would like to know how to customize the ui of a jtextfield so I could create rounded rectangular border without the document going outside the border.
So far I think I have tried the most of what I can think of, I have created a new FieldView class and changed the shape in the paint method according to my customized border that draw rounded rects, the only way I sort of managed to get rid of the white textfield document/view was to set it opaque but I think there should be another way without setting the opaque value.
Have You any experience in customizing the laf of a jtextfield please write back, I have even read the Core Swing advanced book without luck and if you try searching with google please let me know the search phrase as I have tried with keywords like "styling","customizing","ui","plaf","laf" and what not.
I sincerely hope you can give me a nudge in the right direction and I hope nobody will make a flamewar out of this, I have truly used all my resources I can think of.
Sincerely regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想自定义单个组件,实际上在任何组件中都有一种方法可以做到这一点。
请查看本教程:http:// www.eecchhoo.wordpress.com/2012/11/05/screencast-swingmakeover-extreme-java-gui-programming
它包含一个简短的简介和一个下载视频教程的链接。仅供参考,这些视频是印度尼西亚语的,因此如果您在执行该步骤时遇到问题,还有另一个链接可以下载他用来创建教程的项目。
如果您有任何问题,请告诉我。我希望该链接会有所帮助
If you want to customizing single components, there's a way to do that, in any component actually.
Please check this tutorial : http://www.eecchhoo.wordpress.com/2012/11/05/screencast-swingmakeover-extreme-java-gui-programming
It contains a short brief and a link to download the video tutorial. FYI, the videos are in indonesian language, so if you have problem to follow the step, there's also another link to download the project that he used to create the tutorial.
Please tell me if you have any problem. I hope that link will help
我昨天想解决几乎同样的问题,我从你的想法中得到了一些启发,我终于找到了解决方案。
2.为了避免JTextField的四个角出现空白,可以使用
在绘制圆角矩形之前。描边的宽度根据您的需求而定,使其足够宽以覆盖角落的空间。
代码是这样:
效果是这样:
更多详情,可以浏览如何制作圆角矩形JTextField
I wanted to solve almost same problem yesterday and I got some inspiration by your thought and I finally find the solution.
2.To avoid the white space in four corners of JTextField, you can use
before drawing the round rectangle.The width of stroke is based on your demand and just make it wide enough to cover the space in corner.
It's the code:
It's the effect:
For more details , you can browse How to make a round rectangle JTextField
您应该能够使用 Swing 边框来做到这一点。我很久以前就在这里发布了一些关于如何制作圆角边框的代码 - 也许你可以调整它:
http://weblogs.java.net/blog/timboudreau/archive /2005/02/jnn_just_got_pr.html
You should be able to do this with Swing borders. I published some code for how to do rounded borders long ago, here - perhaps you can adapt it:
http://weblogs.java.net/blog/timboudreau/archive/2005/02/jnn_just_got_pr.html