为什么会这样?
据我所知,小部件的行为取决于它的实现,即它是否是一个
-
native widget(赞 org.eclipse.swt.widgets.Text)或
-
custom widget(赞 org.eclipse.swt.custom.StyledText)
区别在于mouse down 或mouse up 事件的处理。
例如,
如果是 org.eclipse.swt.widgets.Text,left mouse down 最终会转换为 OS.SendMessage (hwnd, OS.WM_LBUTTONUP, wParam, lParam);
Whereas
org.eclipse.swt.custom.StyledText 在handleMouseDown(Event event) 方法中使用鼠标事件处理程序和额外处理。大多数功能或 UI 都是使用 custom draw/redraw/validate/invalidate/update 方法完成的。
用很粗俗的win32 sdk方式说:
- windows/win32 GDI提供了一些控件
- 而且,有些是自定义的用户绘制控件
See 下面的 SWT 代码使用 text、styledtext、browser 等进行测试。另外注意浏览器控件并不完全是win32控件,它是Internet Explorer activex或mozilla的gecko引擎的包装控件,therefore其行为与styled text相同。
任何可能的解决方案?
好吧,我只能想到借用 SWT 的 styledtext 代码,然后制作一个适合我的版本。
或者
正如您已经提到的,使用一些侦听器来重置所有其他未聚焦的小部件(即使在我看来这也不是一个非常干净的解决方案)。
测试代码和输出
Code:
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);
}
}