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

6

您可以使用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);
        }
    });
于 2013-10-03T14:41:25.097 回答
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 保持禁用状态。

你怎么能说你在上面的语句中禁用了按钮 A,因为按钮 B 已经被禁用,并且要求单击按钮 B 应该禁用按钮 A。它是一个死锁

于 2013-10-03T14:21:02.943 回答
-1

我建议你使用中介者模式 http://en.wikipedia.org/wiki/Mediator_pattern

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

//Colleague interface
interface Command {
    void execute();
}

//Abstract Mediator
interface IMediator {
    public void book();
    public void view();
    public void search();
    public void registerView(BtnView v);
    public void registerSearch(BtnSearch s);
    public void registerBook(BtnBook b);
    public void registerDisplay(LblDisplay d);
}

//Concrete mediator
class Mediator implements IMediator {

    BtnView btnView;
    BtnSearch btnSearch;
    BtnBook btnBook;
    LblDisplay show;

    //....
    public void registerView(BtnView v) {
        btnView = v;
    }

    public void registerSearch(BtnSearch s) {
        btnSearch = s;
    }

    public void registerBook(BtnBook b) {
        btnBook = b;
    }

    public void registerDisplay(LblDisplay d) {
        show = d;
    }

    public void book() {
        btnBook.setEnabled(false);
        btnView.setEnabled(true);
        btnSearch.setEnabled(true);
        show.setText("booking...");
    }

    public void view() {
        btnView.setEnabled(false);
        btnSearch.setEnabled(true);
        btnBook.setEnabled(true);
        show.setText("viewing...");
    }

    public void search() {
        btnSearch.setEnabled(false);
        btnView.setEnabled(true);
        btnBook.setEnabled(true);
        show.setText("searching...");
    }

}

//A concrete colleague
class BtnView extends JButton implements Command {

    IMediator med;

    BtnView(ActionListener al, IMediator m) {
        super("View");
        addActionListener(al);
        med = m;
        med.registerView(this);
    }

    public void execute() {
        med.view();
    }

}

//A concrete colleague
class BtnSearch extends JButton implements Command {

    IMediator med;

    BtnSearch(ActionListener al, IMediator m) {
        super("Search");
        addActionListener(al);
        med = m;
        med.registerSearch(this);
    }

    public void execute() {
        med.search();
    }

}

//A concrete colleague
class BtnBook extends JButton implements Command {

    IMediator med;

    BtnBook(ActionListener al, IMediator m) {
        super("Book");
        addActionListener(al);
        med = m;
        med.registerBook(this);
    }

    public void execute() {
        med.book();
    }

}

class LblDisplay extends JLabel {

    IMediator med;

    LblDisplay(IMediator m) {
        super("Just start...");
        med = m;
        med.registerDisplay(this);
        setFont(new Font("Arial", Font.BOLD, 24));
    }

}

class MediatorDemo extends JFrame implements ActionListener {

    IMediator med = new Mediator();

    MediatorDemo() {
        JPanel p = new JPanel();
        p.add(new BtnView(this, med));
        p.add(new BtnBook(this, med));
        p.add(new BtnSearch(this, med));
        getContentPane().add(new LblDisplay(med), "North");
        getContentPane().add(p, "South");
        setSize(400, 200);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        Command comd = (Command) ae.getSource();
        comd.execute();
    }

    public static void main(String[] args) {
        new MediatorDemo();
    }

}
于 2013-10-03T14:01:29.447 回答