gpt4 book ai didi

java - 带有单选按钮问题的标签

转载 作者:行者123 更新时间:2023-11-29 06:19:57 24 4
gpt4 key购买 nike

我的代码有一些问题,我看不出逻辑有什么问题,但就是这样。我希望他们可以选择单选按钮或选择一个按钮( radio 按钮),不可用文本区域,反之亦然。这是代码段。当我在单选按钮上来回移动时,两者都变得不可选择,我不确定为什么。

 private void PriceTab()
{
pricePanel = new JPanel(new FlowLayout());


final JRadioButton poolPrice= new JRadioButton("Pool");
final JRadioButton tubPrice = new JRadioButton("Hot Tub");

poolPrice.setSelected(true);

ButtonGroup group = new ButtonGroup();
group.add(poolPrice);
group.add(tubPrice);

pricePanel.add(poolPrice);
pricePanel.add(tubPrice);


final JLabel poolLabel = new JLabel("Enter the pool's volume: ");
final JTextField poolField = new JTextField(10);
pricePanel.add(poolLabel);
pricePanel.add(poolField);

final JTextField tubField = new JTextField(10);
final JLabel tubLabel = new JLabel ("Enter the tub's volume: ");
pricePanel.add(tubLabel);
pricePanel.add(tubField);


JButton calculatePrice = new JButton("Calculate Price");
calculatePrice.setMnemonic('C');
pricePanel.add(calculatePrice);
pricePanel.add(createExitButton());

pricePanel.add(new JLabel("The price is: "));
final JTextField priceField = new JTextField(10);
priceField.setEditable(false);
pricePanel.add(priceField);

final JTextArea messageArea = createMessageArea(1, 25,
"*Please only select one section");
pricePanel.add(messageArea);


calculatePrice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

double pool = Double.parseDouble (poolField.getText());
double tub = Double.parseDouble(tubField.getText());

double price;
if (poolPrice.isSelected()) {
price = pool * 100;
} else {
price = tub * 75;
}
priceField.setText(df.format(price));
}
});



ActionListener priceListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == poolPrice) {
tubLabel.setEnabled(false);
tubField.setEnabled(false);
messageArea.setVisible(false);
} else if (e.getSource() == tubPrice) {
poolLabel.setEnabled(false);
poolField.setEnabled(false);
messageArea.setVisible(false);
}
}
};

poolPrice.addActionListener(priceListener);
tubPrice.addActionListener(priceListener);
}

最佳答案

ActionListener priceListener = new ActionListener() { 
public void actionPerformed(ActionEvent e) {
if (e.getSource() == poolPrice) {
tubLabel.setEnabled(false);
tubField.setEnabled(false);
// Re-enable the previously disabled labels
poolLabel.setEnabled(true);
poolField.setEnabled(true);
messageArea.setVisible(false);
} else if (e.getSource() == tubPrice) {
poolLabel.setEnabled(false);
poolField.setEnabled(false);
// Re-enable disabled labels
tubLabel.setEnabled(true);
tubField.setEnabled(true);

messageArea.setVisible(false);
}
}
};

您需要重新启用您禁用的按钮。

关于java - 带有单选按钮问题的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3539809/

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