当我点击按钮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();
}
}
}
}
答案 0 :(得分:5)
您可以使用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);
}
});
答案 1 :(得分: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()
b
!getSelection()
,就a
致电{1}}。
答案 2 :(得分: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.它是一个死锁
答案 3 :(得分:0)
我不确定你为什么要这样做,但是你不能使用切换按钮:SWT.TOGGLE
答案 4 :(得分:-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();
}
}