Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我应该如何防止tab键激活我的单选按钮,但它仍然应该能够关注单选按钮的文本_Java_User Interface_Swt - Fatal编程技术网

Java 我应该如何防止tab键激活我的单选按钮,但它仍然应该能够关注单选按钮的文本

Java 我应该如何防止tab键激活我的单选按钮,但它仍然应该能够关注单选按钮的文本,java,user-interface,swt,Java,User Interface,Swt,当我在swt中切换向导对话框时,其中包含文本框、单选按钮和按钮。问题是,每当我通过一个禁用的单选按钮(看起来像()radioText)进行tab时,它就会像这样被激活(.):radioText:,除了这个单选按钮之外,还有两个文本框,当单击tab键时,它们的作用是相同的。那个么怎样才能使tab集中在单选按钮上,但不激活它,我的意思是从()radioText状态到(.)radioText状态。有人能分享一下他们在实现这一点上的经验吗。我有一个难看的解决方案,但它是有效的 您可以在单选按钮之前放置另

当我在swt中切换向导对话框时,其中包含文本框、单选按钮和按钮。问题是,每当我通过一个禁用的单选按钮(看起来像()radioText)进行tab时,它就会像这样被激活(.):radioText:,除了这个单选按钮之外,还有两个文本框,当单击tab键时,它们的作用是相同的。那个么怎样才能使tab集中在单选按钮上,但不激活它,我的意思是从()radioText状态到(.)radioText状态。有人能分享一下他们在实现这一点上的经验吗。

我有一个难看的解决方案,但它是有效的

您可以在单选按钮之前放置另一个隐藏控件,而不是让焦点直接到达单选按钮。在我的案例中,我使用了画布。当该控件获得焦点时,获取它后面的单选按钮的选择,将焦点设置为第一个单选按钮,然后将选择恢复到以前的状态。这适用于前向遍历。对于后退(CTRL+TAB),必须对单选按钮后的隐藏控件执行相同的操作

请注意,使用此解决方案,可以在遍历选项卡期间激发选择侦听器。但在它之后,选择将与以前一样

下面是一个实现这一点的小片段。它只在前进模式下工作,画布并没有真正隐藏。你必须为你的案子修正布局

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    new Button(shell, SWT.TOGGLE);
    new Scale(shell, SWT.HORIZONTAL);

    final Canvas invisible = new Canvas(shell, SWT.NONE);
    invisible.addListener(SWT.KeyDown, new Listener() {
        @Override
        public void handleEvent(Event event) {
        }
    });

    final Button radioButton1 = new Button(shell, SWT.RADIO);
    radioButton1.setText("text1");

    final Button radioButton2 = new Button(shell, SWT.RADIO);
    radioButton2.setText("text2");

    invisible.addFocusListener(new FocusListener() {
        @Override
        public void focusGained(FocusEvent e) {
            final boolean selection1 = radioButton1.getSelection();
            final boolean selection2 = radioButton2.getSelection();
            radioButton1.setFocus();
            radioButton1.setSelection(selection1);
            radioButton2.setSelection(selection2);
        }

        @Override
        public void focusLost(FocusEvent e) {
        }
    });

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

此答案适用于单选按钮后有文本字段的情况。在其他情况下,可以使用我的其他答案。它并不完美,也很丑陋,但也许它能解决你的问题

除了内部状态外,我将选择值作为数据(
setData()
)保存,并在每次选择事件后更新正式选择

以下是完整的代码片段:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final Composite composite = new Composite(shell, SWT.NO_RADIO_GROUP);
    composite.setLayout(new GridLayout(2, false));

    new Button(composite, SWT.TOGGLE);
    new Scale(composite, SWT.HORIZONTAL);

    final Button radioButton1 = new Button(composite, SWT.RADIO);
    radioButton1.setText("text1");
    radioButton1.setData("selection", false);
    new Text(composite, SWT.SINGLE);

    final Button radioButton2 = new Button(composite, SWT.RADIO);
    radioButton2.setText("text2");
    radioButton2.setData("selection", false);
    new Text(composite, SWT.SINGLE);

    final Button[] radioGroup = {radioButton1, radioButton2};

    final Listener listener = new Listener() {
        @Override
        public void handleEvent(Event event) {
            final Button button = (Button) event.widget;
            switch (event.type) {
                case SWT.MouseDown:
                    event.widget.setData("selection", true);
                    break;
                case SWT.KeyDown:
                    if (event.keyCode == SWT.SPACE) {
                        event.widget.setData("selection", true);
                    }
                    break;
                case SWT.Selection:
                    final boolean selection = (boolean) button.getData("selection");
                    if (selection != button.getSelection()) {
                        button.setSelection(selection);
                    }
                    if (selection) {
                        for (Button radioButton : radioGroup) {
                            if (radioButton != button) {
                                radioButton.setData("selection", false);
                                radioButton.setSelection(false);
                            }
                        }
                    }
                    break;
            }
        }
    };
    radioButton1.addListener(SWT.MouseDown, listener);
    radioButton1.addListener(SWT.KeyDown, listener);
    radioButton1.addListener(SWT.Selection, listener);
    radioButton2.addListener(SWT.MouseDown, listener);
    radioButton2.addListener(SWT.KeyDown, listener);
    radioButton2.addListener(SWT.Selection, listener);

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

在表单中,如果您想向用户提问,并且该问题需要回答是/否,最好显示空选项

此外,如果用户无法提供答案,则更为清晰,因为他可以清楚地看到表单中遗漏了什么

这确实需要调整(如上面的答案所示)。我通过添加第三个按钮实现了这一点,我人工隐藏了该按钮,并获得了初始选择(即“真”值)

诀窍是不能使用setVisible(false)隐藏按钮,否则,行中的下一个按钮将获得选择


为了解决这个问题,您可以通过截取按钮上的PaintEvents将组件的大小强制为零

我不认为您想说收音机已禁用。可能没有被选中。如果它被禁用,它将无法获得focus.FWIW,我看不到这种行为,至少在Eclipse4.4中的SWT中是这样。具有相同父组合的一组单选按钮将自动分组,这样最多只能选择一个,并且只有选定的一个处于选项卡顺序。下面的解决方法对我来说都不是必需的。同时,它在单选按钮旁边有文本框,文本框在设置文本框焦点时也会将其选择设置为true。有什么办法吗?@suj,我看到复选框工作正常,每当焦点设置在复选框上时,复选框不会被选中,当焦点设置在单选按钮上时,单选按钮的区别是什么?@suj该单选按钮组专为只选择了一个选项的情况而设计的(就像旧屏幕上的按钮,只有当你按下另一个按钮时才会弹出一个)。从理论上讲,您永远不应该有没有选择按钮的单选按钮组。你没有使用单选按钮来设计它们。这就是为什么你需要像上面那样的黑客。我不理解您在评论中提出的第一个问题。我在第一个问题中的意思是,在向导对话框的单选按钮旁边,有两个文本框,通过对它们进行选项卡,可以使tem从非活动状态变为活动状态,如从灰色状态变为非灰色状态state@suj的确如果单选按钮旁边有文本框,则我的解决方案不起作用<代码>设置列表也不起作用。在选择事件中,您并没有源信息来确定它是来自焦点还是来自鼠标单击或按下空格。我可能会放置键和鼠标侦听器,保存最后一个事件,并基于此恢复或不恢复选择。我没有时间测试这个。也许明天吧。是的,你说得对,我试了很多,但都没用。我没有试过按键和鼠标监听器。目前为止,我会尝试任何方式。谢谢分享你的信息。我用其他方式解决了问题,比如把单选按钮放在同一个合成中,它只适用于活动单选按钮状态。我只想和你分享答案,谢谢你的建议和建议code@suj哦,假设你想要一个没有选择按钮的广播组。如果您选择了一个按钮,那么将所有按钮放在同一个组合中显然是一种方法。我喜欢您的答案,在某些情况下可能会有所帮助。很好的尝试
package myexamples;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.wb.swt.layout.grouplayout.GroupLayout;
import org.eclipse.wb.swt.layout.grouplayout.LayoutStyle;
import org.eclipse.swt.widgets.Combo;

public class MySample {
    private static Text text;
    private static Text text_1;
    private static Text text_2;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Shell shell = new Shell();

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setBounds(197, 177, 393, 110);
        composite.setLayout(new GridLayout(2, false));
        new Label(composite, SWT.NONE);
        new Label(composite, SWT.NONE);

        Button btnButton = new Button(composite, SWT.RADIO);
        btnButton.setText(" Button1");

        Composite composite_1 = new Composite(composite, SWT.NONE);
        composite_1.setLayout(new GridLayout(4, false));
        GridData gd_composite_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_composite_1.heightHint = 37;
        gd_composite_1.widthHint = 306;
        composite_1.setLayoutData(gd_composite_1);

        text = new Text(composite_1, SWT.BORDER);
        text.setSize(new Point(10, 8));
        GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        gd_text.widthHint = 72;
        text.setLayoutData(gd_text);

        Label lblLabel = new Label(composite_1, SWT.NONE);
        lblLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        lblLabel.setText("Label2");

        text_1 = new Text(composite_1, SWT.BORDER);
        text_1.setSize(new Point(12, 8));
        GridData gd_text_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        gd_text_1.widthHint = 60;
        text_1.setLayoutData(gd_text_1);

        Label lblLabel_1 = new Label(composite_1, SWT.NONE);
        lblLabel_1.setText(" Label3");
        new Label(composite_1, SWT.NONE);
        new Label(composite_1, SWT.NONE);
        new Label(composite_1, SWT.NONE);
        new Label(composite_1, SWT.NONE);

        Button btnButton_1 = new Button(composite, SWT.RADIO);
        btnButton_1.setText(" Button2");

        Composite composite_2 = new Composite(composite, SWT.NONE);
        composite_2.setLayout(new GridLayout(5, false));
        GridData gd_composite_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_composite_2.widthHint = 305;
        gd_composite_2.heightHint = 30;
        composite_2.setLayoutData(gd_composite_2);

        Combo combo = new Combo(composite_2, SWT.NONE);
        combo.setSize(new Point(10, 8));
        combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        combo.setEnabled(false);
        Combo combo_1 = new Combo(composite_2, SWT.NONE);
        combo_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        combo_1.setEnabled(false);
        Label lblLabelxyz = new Label(composite_2, SWT.NONE);
        lblLabelxyz.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        lblLabelxyz.setText("LabelXYZ1");

        text_2 = new Text(composite_2, SWT.BORDER);
        text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Label lblLabelxyz_1 = new Label(composite_2, SWT.NONE);
        lblLabelxyz_1.setText("LabelXYZ2");
        text_2.setEnabled(false);
        Label label = new Label(shell, SWT.NONE);
        label.setBounds(62, 112, 55, 15);
        label.setText("");
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
Composite c = toolkit.createComposite(parent);
c.setLayout(new RowLayout());

yesButton = toolkit.createButton(c, "Yes", SWT.RADIO);
noButton = toolkit.createButton(c, "No", SWT.RADIO);

yesButton.setSelection(false);
noButton.setSelection(false);

hiddenButton = toolkit.createButton(c, "", SWT.RADIO);
hiddenButton.setSelection(true);
hiddenButton.addPaintListener(new PaintListener() {
    @Override
    public void paintControl(PaintEvent paintevent) {
        // before you paint, shrink the button size to zero
        ((Button)paintevent.widget).setSize(0, 0);
    }
});