gpt4 book ai didi

java - 如何使用多个 StyledText 小部件停止多项选择

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:21 24 4
gpt4 key购买 nike

我正在使用 Java 中的 SWT 开发一个应用程序,并且遇到了多个 StyledText 小部件的一些奇怪行为。这是相当一致的:如果窗口/ View /编辑器中同时显示多个 StyledText 小部件,您可以同时从每个小部件中单独选择您想要的任何内容。在屏幕截图中,有 4 个单独的小部件,有 4 个单独的选择。

a clip of what I mean

我的期望是,如果我开始从一个小部件中进行选择,则可能已经有选择的任何其他小部件都应该丢失它,类似于您期望从网络浏览器中获得的行为;一次只能选择一项。

我想解决这个问题,但我希望避免需要让一些管理器监听每个小部件并在创建新小部件时关闭选择。有一个更好的方法吗? (另外为什么会发生这种情况?)

最佳答案

为什么会发生这种情况?

据我所知,小部件的行为取决于其实现,即它是否是

  1. native 小部件(例如 org.eclipse.swt.widgets.Text)或
  2. 自定义小部件(类似于 org.eclipse.swt.custom.StyledText)

区别在于对鼠标按下鼠标向上事件的处理。

例如,

对于org.eclipse.swt.widgets.Text鼠标左键按下最终会转换为OS.SendMessage (hwnd, OS.WM_LBUTTONUP, wParam, lParam);

org.eclipse.swt.custom.StyledTexthandleMouseDown(Event event) 方法中使用鼠标事件处理程序和额外处理。大多数功能或 UI 都是使用自定义 draw/redraw/validate/invalidate/update 方法完成的。

用一种非常粗略的 win32 sdk 方式来说:

  1. 有一些windows/win32 GDI提供的控件
  2. 而且,有些是自定义的用户绘制控件

参见下面的 SWT 代码,它使用 textstyledtext浏览器 等进行测试。另外注意浏览器控件并不完全是一个 win32 控件,它是 Internet Explorer activex 或 mozilla 的 gecko 引擎的包装控件,因此它的行为与样式文本的行为相同。

有什么可能的解决方案吗?

好吧我只能想到借用SWT的styledtext代码然后制作一个适合我的版本。

或者

正如您已经提到的,使用一些监听器来重置所有其他未聚焦的小部件(即使在我看来这也不是一个非常干净的解决方案)。

测试代码和输出

enter image description here

代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class StyledTextTest {

private static Display display;

public static void main(String[] args)
{
display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2,true));
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

createStyledText(shell);
createStyledText(shell);

createText(shell);
createText(shell);

createCombo(shell);
createCombo(shell);

createCustomCombo(shell);
createCustomCombo(shell);

createBrowser(shell);
createBrowser(shell);

shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

private static void createCustomCombo(Composite parent)
{
new Label(parent, SWT.NONE).setText("Custom Combo");
CCombo c = new CCombo(parent, SWT.DROP_DOWN);
c.setItems(new String[] {"test best", "best rest", "rest test"});
c.select(0);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}

private static void createCombo(Composite parent)
{
new Label(parent, SWT.NONE).setText("Combo");
Combo c = new Combo(parent, SWT.DROP_DOWN);
c.setItems(new String[] {"test best", "best rest", "rest test"});
c.select(0);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}

static void createBrowser(Composite parent)
{
new Label(parent, SWT.NONE).setText("Browser");
Browser browser = new Browser(parent, SWT.NONE);
browser.setText("<div>This is a test !!</div>");
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}

static void createText(Composite parent) {
new Label(parent, SWT.NONE).setText("Text");
final Text text = new Text(parent, SWT.BORDER);
text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");

}

static void createStyledText(Composite parent)
{
new Label(parent, SWT.NONE).setText("Styled Text");

StyledText text = new StyledText (parent, SWT.BORDER|SWT.SINGLE);
text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
// make 0123456789 appear bold
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 10;
style1.fontStyle = SWT.BOLD;
text.setStyleRange(style1);
// make ABCDEFGHIJKLM have a red font
StyleRange style2 = new StyleRange();
style2.start = 11;
style2.length = 13;
style2.foreground = display.getSystemColor(SWT.COLOR_RED);
text.setStyleRange(style2);
// make NOPQRSTUVWXYZ have a blue background
StyleRange style3 = new StyleRange();
style3.start = 25;
style3.length = 13;
style3.fontStyle = SWT.BOLD | SWT.ITALIC;
text.setStyleRange(style3);

}
}

关于java - 如何使用多个 StyledText 小部件停止多项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9489578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com