gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:54:04 26 4
gpt4 key购买 nike

我希望能够在我点击按钮 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com