我有一个包含几个按钮的窗口(窗口充当起始页面)。首次创建并打开using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "sdjkgjkdgjk<br />asdfsdg";
string pattern = "<br.*\\/>"; // Split on <br/>
DisplayByRegex(input, pattern);
input = "sdjkgjkdgjk<br style=\"someAttribute: someProperty;\"/>asdfsdg";
DisplayByRegex(input, pattern);
Console.Read();
}
private static void DisplayByRegex(string input, string pattern)
{
string[] substrings = Regex.Split(input, pattern);
foreach (string match in substrings)
{
Console.WriteLine("'{0}'", match);
}
}
}
时,会为创建的第一个Shell
分配焦点。有没有办法从此Button
删除焦点?
使用Button
无济于事。
答案 0 :(得分:3)
而不是创建虚拟/不必要的按钮你可以只关注你的shell:
shell.open();
shell.forceFocus();
至少在gtk中这样做。
答案 1 :(得分:0)
最简单的解决方法可能是使用以下步骤:
如果您使用的是Button dummy = new Button(parent, SWT.PUSH)
dummy.setFocus()
FormData data = new FormData();
data.bottom = new FormAttachment(-1);
dummy.setLayoutData(data);
:
Composite
这假设父clientArea
的{{1}}占据整个窗口,或位于顶部。
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SOClass {
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
shell.setSize(500, 500);
shell.setText("Remove focus from first button");
shell.setLayout(new FillLayout());
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new FormLayout());
int style = SWT.PUSH | SWT.BORDER;
Button b1 = new Button(comp, style);
Button b2 = new Button(comp, style);
b1.setText("Button 1");
//position: top, spanning the majority of the windows width
FormData b1Data = new FormData();
b1Data.top = new FormAttachment(0, 5);
b1Data.left = new FormAttachment(0, 5);
b1Data.right = new FormAttachment(100, -5);
b1.setLayoutData(b1Data);
b2.setText("Button 2");
//position: under b1, spanning the majority of the windows width
FormData b2Data = new FormData();
b2Data.top = new FormAttachment(b1, 5);
b2Data.left = new FormAttachment(0, 5);
b2Data.right = new FormAttachment(100, -5);
b2.setLayoutData(b2Data);
//try removing/commenting this section to see the effect
Button dummy = new Button(comp, style);
dummy.setFocus();
FormData dummyData = new FormData();
dummyData.bottom = new FormAttachment(-1);
dummy.setLayoutData(dummyData);
shell.open();
while (!shell.isDisposed()) {
if(!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
}
虚拟按钮仍然可用,您仍然可以单击它(物理和编程),但它不可见,因此对于用户来说,它看起来似乎没有为按钮指定焦点。
Button.setVisible(false)
不起作用 - SWT将按钮识别为隐藏,因此SWT会查找下一个按钮以获得焦点。
如果您需要再次关注任何按钮,只需使用dummy.setFocus()