1

ボタン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();
               }
          }
       }
}
4

5 に答える 5

1

どうぞ:

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

    final Button a = new Button(shell, SWT.TOGGLE);
    a.setText("a");
    final Button b = new Button(shell, SWT.PUSH);
    b.setText("b");

    a.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            b.setEnabled(!a.getSelection());
        }
    });

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

setEnabled()ofで呼び出すだけですb!getSelection()a

于 2013-10-03T14:41:08.800 に答える
0

なぜこれを行っているのかわかりませんが、トグルボタンを使用できません:SWT.TOGGLE

于 2013-10-03T14:36:20.617 に答える
0

これが役立つかどうかを確認してください

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);
        case1.setBounds(84, 111, 136, 64);

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

        // Position of the first button
        buttonA.setBounds(10, 10, 147, 16);
        buttonA.setEnabled(true);
        buttonA.setSelection(false);
        buttonA.setText("Button A");

        final Button buttonB = new Button(case1, SWT.CHECK);

        // Position of the second button
        buttonB.setBounds(10, 32, 123, 16);
        buttonB.setEnabled(true);
        buttonB.setSelection(false);
        buttonB.setText("Button B");

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

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

    public static void main(String[] args) {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}

質問を明確にしてください。ボタン(コードごとのチェックボックス)を有効/無効にするか、チェック/チェックを外すことを意味しますか。

私の問題は、ボタンAが有効になるとボタンBが無効になることです(それは大丈夫です)が、ボタンAを無効にするとボタンBは無効のままになります。

ボタン B は既に無効になっているため、上記のステートメントでボタン A を無効にするとどのように言えますか。ボタン B をクリックするとボタン A が無効になる必要があります。デッドロックです。

于 2013-10-03T14:21:02.943 に答える