Java 带有单选按钮的标签问题

Java 带有单选按钮的标签问题,java,swing,Java,Swing,我的代码有一些问题,我看不出逻辑有什么问题,但这里就是。我希望他们有单选按钮来选择或,当选择一个(单选按钮)时,文本区域不可用,反之亦然。下面是代码的一段。当我在单选按钮上来回移动时,两个按钮都变得不可选择,我不确定为什么 private void PriceTab() { pricePanel = new JPanel(new FlowLayout()); final JRadioButton poolPrice= new JRadioButton("Pool"); fin

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

 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);
 }

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

好的,可以。现在,无论出于什么原因,当我只为每一个输入1组数字时,即如果我希望游泳池的体积为2,这意味着热水浴缸将是空白的,我会得到错误。但若我在热水浴池里填一个数字,在游泳池里填两个,我得到的价格是200。线程“AWT-EventQueue-0”java.lang.NumberFormatException中的异常:sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)处的空字符串位于java.lang.Double.parseDouble(Double.java:510)处la.GUI$1.actionPerformed(la.java:154)Double pool=Double.parseDouble(poolField.getText());double-tub=double.parseDouble(tubField.getText());如果其中一个文本字段为空,则会出现numberformatexception,因为无法将空字符串(“”)转换为数字。你需要处理那个案子。
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); 
            } 
            } 
        };