java - 通过启用另一个按钮来禁用一个按钮,反之亦然

标签 java button swt

我希望能够在我点击按钮 A(以及相反的按钮)时禁用按钮 B。

我的问题是,一旦启用按钮 A,按钮 B 就会被禁用(没关系)但是当我禁用按钮 A 时,按钮 B 会保持禁用状态。

我该如何解决?

这里我举了一个例子来帮助理解这个问题:

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

public class Buttons 
{
    protected static Shell shell;

    protected static void createContents() 
    {
       // Creation of the window
       shell = new Shell();
       shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
       shell.setSize(800, 600);
       shell.setText("Test");
       shell.setLayout(null);

       // Creation of the background canvas
       Canvas area1 = new Canvas(shell, SWT.NONE);
       area1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
       area1.setBounds(82, 119, 597, 393);
       buttons(area1);
    }
    public static void buttons(Canvas area1)
    {
       // Creation of the canvas to color the text area from the buttons
       Canvas case1 = new Canvas(area1, SWT.NONE);

       // Creation of the buttons
       final Button buttonA = new Button(case1, SWT.CHECK);
       final Button buttonB = new Button(case1, SWT.CHECK);

       // Position of the first button
       case1.setBounds(84, 111, 136, 64);
       buttonA.setBounds(10, 10, 147, 16);
       buttonA.addSelectionListener(new SelectionAdapter() 
       {
           @Override
           public void widgetSelected(SelectionEvent arg0) 
            {
               boolean chkRpmFilter = buttonA.getSelection();
               buttonB.setEnabled(false);
               System.out.println("Button A clicked state is " +chkRpmFilter);
            }
        });
       buttonB.setEnabled(true);
       buttonA.setText("Button A");

       // Position of the second button
       buttonB.setBounds(10, 32, 123, 16);
       buttonB.addSelectionListener(new SelectionAdapter() 
       {
             @Override
             public void widgetSelected(SelectionEvent arg0) 
             {
                boolean sWinddirections = buttonB.getSelection();
                buttonA.setEnabled(false);
                System.out.println("Button B clicked state is " +sWinddirections);
              }
       });
       buttonA.setEnabled(true);
       buttonB.setText("Button B");
       }
       public static void main(String[] args) 
       {
         Display display = Display.getDefault();
         createContents();
         shell.open();
         shell.layout();
         while (!shell.isDisposed()) 
         {
           if (!display.readAndDispatch()) 
              {
                display.sleep();
               }
          }
       }
}

最佳答案

您可以使用 getSelection() 来了解按钮 A 是否被选中以重新启用第二个按钮。

   buttonA.addSelectionListener(new SelectionAdapter() 
   {
       @Override
       public void widgetSelected(SelectionEvent arg0) 
        {
           boolean chkRpmFilter = buttonA.getSelection();
           buttonB.setEnabled(!buttonA.getSelection());
           System.out.println("Button A clicked state is " +chkRpmFilter);
        }
    });

关于java - 通过启用另一个按钮来禁用一个按钮,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161181/

相关文章:

java - 如何从正则表达式中提取值并打印它们并对这些值进行计算

java - SWT CoolBar 工具栏右侧位置

java - 如何使用带有复选框 SWT 按钮的自动文本绑定(bind)

java - 将静态方法传递给按钮工厂 (Java)

java - 设计一个主题工厂

java - 如何对排列进行排序

java - 如何设置 jCaptcha 生成的验证码中文本的字体样式

javascript - 单击清除文本区域

swift - 填充 Collection View 时如何判断是否选择了 Collection View 标题中的按钮?

button - 汉堡菜单图标和三个垂直点图标叫什么?