gpt4 book ai didi

Java Applet 复选框错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:53 25 4
gpt4 key购买 nike

我的 java 复选框小程序遇到问题。我基本上可以正常工作,但最终遇到了一个小错误。当我选择汤时,沙拉应该变灰(确实如此),但如果我已经从沙拉中选择了一个项目,然后单击汤,沙拉就会变灰,并且该项目(沙拉内)仍然被选中。

public class Applet_11 extends Applet implements ItemListener
{
Panel
dinnerPanel,
soupPanel,
saladPanel;

Checkbox
standard, deluxe,
soup, salad,
cream, broth, gumbo,
tossed, caesar,
croutons,lite;

//Used to setup all of the boxes and add them to the Applet
public void init()
{
CheckboxGroup dinnerType = new CheckboxGroup();
standard = new Checkbox("standard", dinnerType, false);
standard.addItemListener(this);
deluxe = new Checkbox("deluxe", dinnerType, true);
deluxe.addItemListener(this);

CheckboxGroup soupOrSalad = new CheckboxGroup();
soup = new Checkbox("Soup", soupOrSalad, false);
soup.addItemListener(this);
salad = new Checkbox("Salad", soupOrSalad, false);
salad.addItemListener(this);

CheckboxGroup soups = new CheckboxGroup();
cream = new Checkbox("cream", soups, false);
cream.addItemListener(this);
broth = new Checkbox("broth", soups, false);
broth.addItemListener(this);
gumbo = new Checkbox("gumbo", soups, false);
gumbo.addItemListener(this);

CheckboxGroup salads = new CheckboxGroup();
tossed = new Checkbox("tossed", salads, false);
tossed.addItemListener(this);
caesar = new Checkbox("ceasar", salads, false);
caesar.addItemListener(this);
croutons = new Checkbox("croutons", salads, false);
croutons.addItemListener(this);
lite = new Checkbox("lite", salads, false);
lite.addItemListener(this);

//adding the componets to the Applet
setLayout(new GridLayout(0, 1));

dinnerPanel = new Panel();
add(dinnerPanel);
dinnerPanel.add(standard);
dinnerPanel.add(deluxe);

soupPanel = new Panel();
add(soupPanel);
soupPanel.add(soup);
soupPanel.add(cream);
soupPanel.add(broth);
soupPanel.add(gumbo);

saladPanel = new Panel();
add(saladPanel);
saladPanel.add(salad);
saladPanel.add(tossed);
saladPanel.add(caesar);
saladPanel.add(croutons);
saladPanel.add(lite);
}

//ItemEvents and calls other functions
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == standard || e.getSource() == deluxe) {
handleDinnerType((Checkbox)e.getSource());
} else if (e.getSource() == soup || e.getSource() == salad) {
handleSoupSaladChoice((Checkbox)e.getSource());
}
}

//helper function for dinner type
void handleDinnerType(Checkbox selectedType)
{
boolean enabled = false;

if (selectedType == standard) {
enabled = false;
} else if (selectedType == deluxe) {
enabled = true;
}

cream.setEnabled(enabled);
broth.setEnabled(enabled);
gumbo.setEnabled(enabled);
tossed.setEnabled(enabled);
caesar.setEnabled(enabled);
croutons.setEnabled(enabled);
lite.setEnabled(enabled);

}

//helper function for salad and soup type
void handleSoupSaladChoice(Checkbox selectedCourse)
{
boolean soupEnabled, saladEnabled;

if (selectedCourse == soup) {
soupEnabled = true;
saladEnabled = false;
} else {
soupEnabled = false;
saladEnabled = true;
}

cream.setEnabled(soupEnabled);
broth.setEnabled(soupEnabled);
gumbo.setEnabled(soupEnabled);

tossed.setEnabled(saladEnabled);
caesar.setEnabled(saladEnabled);
croutons.setEnabled(saladEnabled);
lite.setEnabled(saladEnabled);
}
}

最佳答案

您所询问的行为是半复选框和半单选按钮,有两种方法可以解决它:

  1. 在复选框组中添加一个额外的复选框,基本上表示“没有其他选项”。并在监听器中设置该复选框的状态。因为我相信一旦您在复选框组中选择了一个选项,您就无法清除它。

  2. 删除复选框组并让您的监听器管理其他复选框的排除,而不是依赖于复选框组。示例代码是

-

public class Applet_15934232 extends Applet implements ItemListener {
Panel
dinnerPanel,
soupPanel,
saladPanel;

Checkbox
standard, deluxe,
soup, salad,
cream, broth, gumbo,
tossed, caesar,
croutons, lite;

//Used to setup all of the boxes and add them to the Applet
public void init() {
CheckboxGroup dinnerType = new CheckboxGroup();
standard = new Checkbox("standard", dinnerType, false);
standard.addItemListener(this);
deluxe = new Checkbox("deluxe", dinnerType, true);
deluxe.addItemListener(this);

CheckboxGroup soupOrSalad = new CheckboxGroup();
soup = new Checkbox("Soup", soupOrSalad, false);
soup.addItemListener(this);
salad = new Checkbox("Salad", soupOrSalad, false);
salad.addItemListener(this);

cream = new Checkbox("cream", false);
cream.addItemListener(this);
broth = new Checkbox("broth", false);
broth.addItemListener(this);
gumbo = new Checkbox("gumbo", false);
gumbo.addItemListener(this);

tossed = new Checkbox("tossed", false);
tossed.addItemListener(this);
caesar = new Checkbox("ceasar", false);
caesar.addItemListener(this);
croutons = new Checkbox("croutons", false);
croutons.addItemListener(this);
lite = new Checkbox("lite", false);
lite.addItemListener(this);


//adding the componets to the Applet
setLayout(new GridLayout(0, 1));

dinnerPanel = new Panel();
add(dinnerPanel);
dinnerPanel.add(standard);
dinnerPanel.add(deluxe);

soupPanel = new Panel();
add(soupPanel);
soupPanel.add(soup);
soupPanel.add(cream);
soupPanel.add(broth);
soupPanel.add(gumbo);

saladPanel = new Panel();
add(saladPanel);
saladPanel.add(salad);
saladPanel.add(tossed);
saladPanel.add(caesar);
saladPanel.add(croutons);
saladPanel.add(lite);
}

//ItemEvents and calls other functions
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == standard || e.getSource() == deluxe) {
handleDinnerType((Checkbox) e.getSource());
} else if (e.getSource() == soup || e.getSource() == salad) {
handleSoupSaladChoice((Checkbox) e.getSource());
} else {
handleButtonSelection((Checkbox) e.getSource());
}
}

private void handleButtonSelection(Checkbox source) {
cream.setState(source == cream);
broth.setState(source == broth);
gumbo.setState(source == gumbo);
tossed.setState(source == tossed);
caesar.setState(source == caesar);
croutons.setState(source == croutons);
lite.setState(source == lite);
}

//helper function for dinner type
void handleDinnerType(Checkbox selectedType) {
boolean enabled = false;

if (selectedType == standard) {
enabled = false;
} else if (selectedType == deluxe) {
enabled = true;
}

cream.setEnabled(enabled);
broth.setEnabled(enabled);
gumbo.setEnabled(enabled);
tossed.setEnabled(enabled);
caesar.setEnabled(enabled);
croutons.setEnabled(enabled);
lite.setEnabled(enabled);

}

//helper function for salad and soup type
void handleSoupSaladChoice(Checkbox selectedCourse) {
boolean soupEnabled = (selectedCourse == soup);
boolean saladEnabled = (selectedCourse == salad);

if (soupEnabled) {
tossed.setState(false);
caesar.setState(false);
croutons.setState(false);
lite.setState(false);
}
if (saladEnabled) {
cream.setState(false);
broth.setState(false);
gumbo.setState(false);
}


cream.setEnabled(soupEnabled);
broth.setEnabled(soupEnabled);
gumbo.setEnabled(soupEnabled);

tossed.setEnabled(saladEnabled);
caesar.setEnabled(saladEnabled);
croutons.setEnabled(saladEnabled);
lite.setEnabled(saladEnabled);
}
}

关于Java Applet 复选框错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934232/

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