标签与单选按钮问题

时间:2010-08-22 02:11:40

标签: java swing

我的代码存在一些问题,我无法看到逻辑上的错误,但现在是。我希望他们有单选按钮来选择或,当选择一个(单选按钮)时,文本区域不可用,反之亦然。这是代码的一部分。当我在收音机按钮上来回走动时,两者都变得不可选,我不确定为什么。

 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 个答案:

答案 0 :(得分:3)

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

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