SWT/WindowBuilder 中的多行按钮文本?

发布于 2024-10-09 14:41:21 字数 240 浏览 5 评论 0原文

有没有办法强制 SWT 按钮文本跨两行,无论是在 SWT 代码中手动还是使用 WindowBuilder GUI? 我有这个: alt text

我想要这样的东西(经过数字更改的图像来实现此目的): alt text

这可能吗?如何?

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:
alt text

I want something like this (digitally altered image to achieve this):
alt text

Is it possible? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

残龙傲雪 2024-10-16 14:41:21

通过添加 SWT.WRAP 作为按钮的样式,并在要设置为按钮标签的文本中插入 '\n',我能够使多行按钮文本工作。

....
Button check = new Button(composite, SWT.WRAP | SWT.PUSH );
check.setText("Multi\n Line\n Button \n Text");
....

即使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.

....
Button check = new Button(composite, SWT.WRAP | SWT.PUSH );
check.setText("Multi\n Line\n Button \n Text");
....

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.

enter image description here

友谊不毕业 2024-10-16 14:41:21

我不会深入研究本机代码,而是通过创建自定义控件来实现此目的。扩展复合,为多行文本使用标签,并使用 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.

云淡风轻 2024-10-16 14:41:21

下面的代码可以解决这个问题:

final int style = OS.GetWindowLong(button.handle, OS.GWL_STYLE);
OS.SetWindowLong(button.handle, OS.GWL_STYLE, style | OS.BS_MULTILINE);
button.setText("line 1\nline 2");

您只需导入 org.eclipse.swt.internal.win32.OS 即可。当然,该类以及按钮内的句柄字段不是 SWT API 的一部分,因此您的代码不再可移植。但这些函数是在 Windows API 中定义的,因此您不必太担心它们会在 SWT 的未来版本中发生变化。

请注意,进行此更改后,computeSize() 不再正常工作。


编辑:我负责computeSize()和GWL风格的完整课程

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;


public class MultilineButton extends Button {

    public MultilineButton(Composite parent, int style) {
        super(parent, style);
        final int gwlStyle = OS.GetWindowLong(this.handle, OS.GWL_STYLE);
        OS.SetWindowLong(this.handle, OS.GWL_STYLE, gwlStyle | OS.BS_MULTILINE);
    }

    @Override
    protected void checkSubclass() {
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        final Point size = super.computeSize(wHint, hHint, changed);
        final GC gc = new GC(this);

        final String multiLineText = this.getText();
        final Point multiLineTextSize = gc.textExtent(multiLineText, SWT.DRAW_DELIMITER);

        final String flatText = multiLineText.replace('\n', ' ');
        final Point flatTextSize = gc.textExtent(flatText);

        gc.dispose();

        size.x -= flatTextSize.x - multiLineTextSize.x;
        size.y += multiLineTextSize.y - flatTextSize.y;

        return size;
    }
}

在此处输入图像描述

The following code does the trick:

final int style = OS.GetWindowLong(button.handle, OS.GWL_STYLE);
OS.SetWindowLong(button.handle, OS.GWL_STYLE, style | OS.BS_MULTILINE);
button.setText("line 1\nline 2");

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;


public class MultilineButton extends Button {

    public MultilineButton(Composite parent, int style) {
        super(parent, style);
        final int gwlStyle = OS.GetWindowLong(this.handle, OS.GWL_STYLE);
        OS.SetWindowLong(this.handle, OS.GWL_STYLE, gwlStyle | OS.BS_MULTILINE);
    }

    @Override
    protected void checkSubclass() {
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        final Point size = super.computeSize(wHint, hHint, changed);
        final GC gc = new GC(this);

        final String multiLineText = this.getText();
        final Point multiLineTextSize = gc.textExtent(multiLineText, SWT.DRAW_DELIMITER);

        final String flatText = multiLineText.replace('\n', ' ');
        final Point flatTextSize = gc.textExtent(flatText);

        gc.dispose();

        size.x -= flatTextSize.x - multiLineTextSize.x;
        size.y += multiLineTextSize.y - flatTextSize.y;

        return size;
    }
}

enter image description here

野鹿林 2024-10-16 14:41:21

我会尝试将按钮文本设置为带有“\n”字符的字符串,您希望在其中发生换行。

I would try setting the button text to a string with a '\n' character where you want the line break to happen.

孤君无依 2024-10-16 14:41:21

实际上,swing 有一种超级简单的方法,而不是 SWT。当我第一次发布这个答案时,我误读了这个问题,但由于我在寻找摆动解决方案时找到了这里的方法,所以我将把它留给其他人。

button.setText("<html>My<br>Text<br>");

许多 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.

button.setText("<html>My<br>Text<br>");

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.

谈场末日恋爱 2024-10-16 14:41:21

我能想到的唯一方法是通过 JNI/JNA。请注意,如果您沿着这条路走下去,您将通过调用特定于平台的本机函数来破坏 Java 的平台独立性。

对于 Windows,请查看 SetWindowText API 调用。假设您有类似以下的代码:

Button btn = new Button(shell, SWT.PUSH);
btn.setText("Hello world!");

您可以通过btn.handle获取句柄。有了一点 JNA 的魔力,这些事情可能是可能的:

final User32 lib = (User32) Native.loadLibrary("user32", User32.class);
final HWND hWnd = new W32API.HWND(new W32API.UINT_PTR(btn.handle)
        .toPointer());
final String text = "Hello\nworld!"; // or "Hello\r\nworld!"?
lib.SetWindowText(hWnd, text.toCharArray());

我实际上无法让它工作(它在 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:

Button btn = new Button(shell, SWT.PUSH);
btn.setText("Hello world!");

You can obtain the handle via btn.handle. With a bit of JNA magic something along these lines might be possible:

final User32 lib = (User32) Native.loadLibrary("user32", User32.class);
final HWND hWnd = new W32API.HWND(new W32API.UINT_PTR(btn.handle)
        .toPointer());
final String text = "Hello\nworld!"; // or "Hello\r\nworld!"?
lib.SetWindowText(hWnd, text.toCharArray());

I couldn't actually get this to work (it gives a java.lang.UnsatisfiedLinkError on SetWindowText() which I couldn't sort out), but this might point you in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文