SWT/WindowBuilder 中的多行按钮文本?
有没有办法强制 SWT 按钮文本跨两行,无论是在 SWT 代码中手动还是使用 WindowBuilder GUI? 我有这个:
我想要这样的东西(经过数字更改的图像来实现此目的):
这可能吗?如何?
Is there any way to force the SWT button text to span two lines, either manually in the SWT code or using the WindowBuilder GUI?
I have this:
I want something like this (digitally altered image to achieve this):
Is it possible? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过添加 SWT.WRAP 作为按钮的样式,并在要设置为按钮标签的文本中插入 '\n',我能够使多行按钮文本工作。
即使button.setText(..)java文档说按钮标签不能包含换行符,它在SWT.WRAP添加为样式时起作用(未设置SWT.WRAP时'\n'被忽略) 。
只是觉得这可能有用。
I was able to get the multi-line button text work, by adding SWT.WRAP as a style for the button and by having '\n' inserted in the text to be set as the button label.
Even though the button.setText(..) java doc says that the button label must not contain new line chars, it works when SWT.WRAP is added as a style('\n' were ignored when SWT.WRAP was not set).
Just thought this could be useful.
我不会深入研究本机代码,而是通过创建自定义控件来实现此目的。扩展复合,为多行文本使用标签,并使用 2D 绘图添加一些装饰。图形上下文中有用于圆角矩形和渐变绘制的方法。它可能看起来不太像本机小部件,但在我看来,它比使用 JNI 更好。
Instead of diving into native code, I would do this by creating a custom control. Extend composite, use a label for the multi-line text and add some decoration with 2D drawing. There are methods in graphic context for rounded rectangles and gradient painting. It may not look exactly like a native widget, but, in my opinion, better than using JNI.
下面的代码可以解决这个问题:
您只需导入 org.eclipse.swt.internal.win32.OS 即可。当然,该类以及按钮内的句柄字段不是 SWT API 的一部分,因此您的代码不再可移植。但这些函数是在 Windows API 中定义的,因此您不必太担心它们会在 SWT 的未来版本中发生变化。
请注意,进行此更改后,computeSize() 不再正常工作。
编辑:我负责computeSize()和GWL风格的完整课程
The following code does the trick:
You just import org.eclipse.swt.internal.win32.OS and that's it. Of course, that class, and the handle field inside button are not part of SWT API, so your code is no longer portable. But the functions are defined in Windows API, so you don't have to worry too much they will change in future versions of SWT.
Be aware that after this change, computeSize() no longer works well.
Edit: full class where I take care of computeSize() and GWL style
我会尝试将按钮文本设置为带有“\n”字符的字符串,您希望在其中发生换行。
I would try setting the button text to a string with a '\n' character where you want the line break to happen.
实际上,swing 有一种超级简单的方法,而不是 SWT。当我第一次发布这个答案时,我误读了这个问题,但由于我在寻找摆动解决方案时找到了这里的方法,所以我将把它留给其他人。
许多 swing 组件都会渲染 HTML。
在检查发布日期时,不确定这是否是一个老问题的相关答案,但我在尝试解决这个问题时发现了这一点,所以我想我会分享。干杯。
there's actually a super easy way in swing, not in SWT. I misread the question when I first posted this answer, but since I found my way here whilst searching for a swing solution, I'll leave it up for others.
many swing components render HTML.
On checking the date of posting, not sure if this is a relevant answer to a question that old, but I found this while trying to solve this problem so I thought I'd share. Cheers.
我能想到的唯一方法是通过 JNI/JNA。请注意,如果您沿着这条路走下去,您将通过调用特定于平台的本机函数来破坏 Java 的平台独立性。
对于 Windows,请查看 SetWindowText API 调用。假设您有类似以下的代码:
您可以通过
btn.handle
获取句柄。有了一点 JNA 的魔力,这些事情可能是可能的:我实际上无法让它工作(它在
SetWindowText()
上给出了java.lang.UnsatisfiedLinkError
我无法解决),但这可能会为您指明正确的方向。The only way I can think of doing this, is through JNI/JNA. Take note that if you go down this road, you'll be breaking Java's platform independence by calling platform-specific native functions.
For Windows, take a look at the SetWindowText API call. Supposing you have code similar to:
You can obtain the handle via
btn.handle
. With a bit of JNA magic something along these lines might be possible:I couldn't actually get this to work (it gives a
java.lang.UnsatisfiedLinkError
onSetWindowText()
which I couldn't sort out), but this might point you in the right direction.