2015-05-06 88 views
2

我需要編寫一個應用程序,允許用戶在JCheckBoxes中選擇保險選項。選擇JCheckBox後,我想將該選項的值添加到「TotalPrice」中。我將totalPrice初始化爲0,但是當我嘗試在「if」語句中添加它時,沒有任何反應。當選擇JCheckBox時添加到變量

package p3s4; 

import java.text.*; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class JInsurance extends JFrame implements ItemListener { 
    //initialize variables. 
    FlowLayout flow = new FlowLayout(); 
    int hmoPrice = 200; 
    int ppoPrice = 600; 
    int dentalPrice = 75; 
    int visionPrice = 20; 
    String decimal = ".00"; 
    DecimalFormat moneyFormat = new DecimalFormat(decimal); 
    int totalPrice = 0; 
    //initialize TextFields and CheckBoxes. 
    JLabel heading = new JLabel("Choose insurance options: "); 
    JCheckBox hmo = new JCheckBox("HMO"); 
    JCheckBox ppo = new JCheckBox("PPO"); 
    ButtonGroup providersGroup = new ButtonGroup(); 
    JCheckBox dental = new JCheckBox("Dental"); 
    JCheckBox vision = new JCheckBox("Vision"); 
    JTextField hmoSelection = new JTextField(hmo.getText() + " $" + moneyFormat.format(hmoPrice)); 
    JTextField ppoSelection = new JTextField(ppo.getText() + " $" + moneyFormat.format(ppoPrice)); 
    JTextField dentalSelection = new JTextField(dental.getText() + " $" + moneyFormat.format(dentalPrice)); 
    JTextField visionSelection = new JTextField(vision.getText() + " $" + moneyFormat.format(visionPrice)); 
    JTextField totalSelection = new JTextField("Total: $" + totalPrice); 

    public JInsurance() { 
     super("Insurance Options"); 

     setLayout(flow); 
     add(heading); 
     providersGroup.add(hmo); 
     providersGroup.add(ppo); 
     add(hmo); 
     add(ppo); 
     add(dental); 
     add(vision); 
     add(totalSelection); 
     add(hmoSelection); 
     add(ppoSelection); 
     add(dentalSelection); 
     add(visionSelection); 
     //Set visibility. 
     hmoSelection.setVisible(false); 
     ppoSelection.setVisible(false); 
     dentalSelection.setVisible(false); 
     visionSelection.setVisible(false); 
     totalSelection.setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //add listeners. 
     hmo.addItemListener(this); 
     ppo.addItemListener(this); 
     dental.addItemListener(this); 
     vision.addItemListener(this); 
    } 

    public void itemStateChanged(ItemEvent event) { 
     Object source = event.getSource(); 
     //Check if boxes are selected. If they are, add price of insurance 
     //option to "totalPrice" TextField. 
     if (source == hmo) { 
      if (event.getStateChange() == ItemEvent.SELECTED) { 
       totalPrice += 200; 
       hmoSelection.setEditable(false); 
       hmoSelection.setVisible(true); 
       hmoSelection.getParent().revalidate(); 

      } else { 
       totalPrice -= 200; 
       hmoSelection.setVisible(false); 
      } 
     } else if (source == ppo) { 
      if (event.getStateChange() == ItemEvent.SELECTED) { 
       totalPrice += 600; 
       ppoSelection.setEditable(false); 
       ppoSelection.setVisible(true); 
       ppoSelection.getParent().revalidate(); 
      } else { 
       totalPrice -= 600; 
       ppoSelection.setVisible(false); 
      } 
     } else if (source == dental) { 
      if (event.getStateChange() == ItemEvent.SELECTED) { 
       totalPrice += 75; 
       dentalSelection.setEditable(false); 
       dentalSelection.setVisible(true); 
       dentalSelection.getParent().revalidate(); 
      } else { 
       totalPrice -= 75; 
       dentalSelection.setVisible(false); 
      } 
     } else { 
      if (event.getStateChange() == ItemEvent.SELECTED) { 
       totalPrice += 20; 
       visionSelection.setEditable(false); 
       visionSelection.setVisible(true); 
       visionSelection.getParent().revalidate(); 
      } else { 
       totalPrice -= 20; 
       visionSelection.setVisible(false); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     JInsurance first = new JInsurance(); 
     final int WIDTH = 500; 
     final int HEIGHT = 300; 
     first.setSize(WIDTH, HEIGHT); 
     first.setVisible(true); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     first.setLocation(dim.width/2 - first.getSize().width/2, 
       dim.height/2 - first.getSize().height/2); 
    } 
} 
+0

結束時復位的totalSelection文'沒什麼happens'你期望發生的?你想'totalSelection'中的值改變嗎? – copeg

+0

是的,這正是我想要發生的事情。 – Eleyna

回答

1

您需要自行更新totalSelection的文字。這是因爲當你初始化它時,你提供了一個當時由totalPrice的值生成的String對象。

因此,要解決這個問題,在itemStateChanged

+0

謝謝,這解決了我的問題:) – Eleyna

0

您顯示totalSelection的JTextField內totalPrice值,但你永遠不更新由JTextField中持有的文本。 Java編譯器和JVM非常笨,只會做你告訴它做的事情。如果您希望更新文本,則當您希望更新發生時,您必須在文本字段上明確呼叫setText(...)

+0

謝謝!很有幫助。 – Eleyna

相關問題