启用另一个按钮禁用一个按钮,反之亦然

tro*_*guy 1 java swt button

当我点击按钮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();
               }
          }
       }
}
Run Code Online (Sandbox Code Playgroud)

flu*_*nis 5

您可以使用getSelection()以确定是否检查buttonA是否重新启用第二个按钮.

   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);
        }
    });
Run Code Online (Sandbox Code Playgroud)