2014-12-02 77 views
0

我正在創建GUI程序叫做KwH計算器,它所做的是計算某個項目的耗電量,所以我的問題是在組合框中,我想要的是,如果用戶選擇一個項目在選擇中,瓦特將自動出現在功耗(powTF)上。JCombo Box增加值

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

public class KwH extends JFrame{ 
    private JComboBox appCB; 
    private JLabel appL,powL,unitL,wattsL,hrL,hoursL,dayL,monthL,yearL,kwhdayL,kwhmonthL,kwhyearL; 
    private JTextField appTF,powTF,hoursTF,dayTF,monthTF,yearTF; 
    private JButton calculateB,exitB,resetB; 
    private CalculateButtonHandler cbHandler; 
    private ExitButtonHandler exitHandler; 
    private ResetButtonHandler resetHandler; 
    private String[] appStrings={" ","Refrigirator","Electric Iron","Fan","Television","Washing Machine"}; 

public KwH(){ 


    appCB=new JComboBox(appStrings); 
    appL=new JLabel ("Appliances: ",SwingConstants.LEFT); 
    powL=new JLabel ("Power Consumption: ",SwingConstants.LEFT); 
    hoursL=new JLabel ("Hours of Use: " ,SwingConstants.LEFT); 
    unitL=new JLabel (" ",SwingConstants.LEFT); 
    wattsL=new JLabel ("Watts",SwingConstants.LEFT); 
    hrL=new JLabel("hr/day",SwingConstants.LEFT); 
    dayL=new JLabel ("Energy Consumed per Day: ",SwingConstants.LEFT); 
    monthL=new JLabel("Energy Consumed per Month: ",SwingConstants.LEFT); 
    yearL=new JLabel("Energy Consumed per Year: ",SwingConstants.LEFT); 
    kwhdayL=new JLabel("kwh/day" ,SwingConstants.LEFT); 
    kwhmonthL=new JLabel("kwh/month",SwingConstants.LEFT); 
    kwhyearL=new JLabel("kwh/year", SwingConstants.LEFT); 


    appTF=new JTextField(10); 
    powTF=new JTextField(10); 
    hoursTF=new JTextField(10); 
    dayTF=new JTextField(10);; 
    monthTF=new JTextField(10); 
    yearTF=new JTextField(10); 


    calculateB=new JButton("Calculate"); 
    cbHandler=new CalculateButtonHandler(); 
    calculateB.addActionListener(cbHandler); 

    exitB=new JButton("Exit"); 
    exitHandler=new ExitButtonHandler(); 
    exitB.addActionListener(exitHandler); 

    resetB=new JButton("Reset"); 
    resetHandler=new ResetButtonHandler(); 
    resetB.addActionListener(resetHandler); 


    setVisible(true); 
    setTitle("KwH Calculator"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(600,600); 

    Container p=getContentPane(); 
    p.setLayout(new GridLayout(7,3)); 


    p.add(appL); 
    p.add(appCB); 
    p.add(unitL); 
    p.add(powL); 
    p.add(powTF); 
    p.add(wattsL); 
    p.add(hoursL); 
    p.add(hoursTF); 
    p.add(hrL); 
    p.add(calculateB); 
    p.add(exitB); 
    p.add(resetB); 
    p.add(dayL); 
    p.add(dayTF); 
    p.add(kwhdayL); 
    p.add(monthL); 
    p.add(monthTF); 
    p.add(kwhmonthL); 
    p.add(yearL); 
    p.add(yearTF); 
    p.add(kwhyearL); 


} 

public class CalculateButtonHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 

     double a,b,c,d,f,g,h; 

     a=Double.parseDouble(powTF.getText()); 
     b=Double.parseDouble(hoursTF.getText()); 


     c=(a*b)/1000; 
     d=c*30; 
     f=d*12; 

     dayTF.setText(""+c); 
     monthTF.setText(""+d); 
     yearTF.setText(""+f); 


    } 
} 

public class ResetButtonHandler implements ActionListener{ 
    public void actionPerformed (ActionEvent e){ 

    } 
} 

public class ExitButtonHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     System.exit(0); 
    } 
} 

public static void main(String[]args){ 

    KwH p=new KwH(); 
} 

} 
+3

使用JCOmponents可以返回雙精度值,休息爲(其型號)設置爲從ActionListener的 – mKorbel 2014-12-02 13:39:33

+0

@Patrick路易·費雷爾解析接受雙重價值,無副作用感謝發佈功能代碼。不完全確定你想要什麼。當您更改組合框項目時,是否希望程序自動填充「功耗」?或者你只是想重新計算? – Chris 2014-12-02 14:07:09

+0

@Chris我想要的是,如果用戶選擇一個項目,等效瓦特將自動顯示功耗,例如,如果我選擇冰箱,那麼在功耗上必須出現200瓦的值。那麼用戶將輸入使用小時數,然後計算 – 2014-12-02 14:16:25

回答

2

以此爲例,用戶仍然需要輸入功耗。與當前的設計保持一致,添加您的功耗值的數組:

private String[] appStrings={" ","Refrigirator","Electric Iron","Fan","Television","Washing Machine"}; 
private double[] appPower={0.0, 200.0, 50.0, 20.0, 150.0, 250.0}; // New 

添加ActionChangeListener到您的組合框:

appCB.addActionListener(new ApplianceChangeListener()); 

既然你有一個查找表,您可能不希望用戶編輯功耗。如果是這樣的話,你可能要設置的字段不可編輯:

powTF.setEditable(false); 

爲了保持計算按鈕的工作和添加功能,我建議全部來自CalculateButtonHandler代碼移動到一個新的方法稱爲計算,並只需按鈕處理程序調用計算。

public class CalculateButtonHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     calculate(); 
    } 
} 

private void calculate() { 
    double a,b,c,d,f,g,h; 

    try { 
     a=Double.parseDouble(powTF.getText()); 
     b=Double.parseDouble(hoursTF.getText()); 
    } catch (NumberFormatException nfe) { 
     return; 
    } 

    c=(a*b)/1000; 
    d=c*30; 
    f=d*12; 

    dayTF.setText(""+c); 
    monthTF.setText(""+d); 
    yearTF.setText(""+f); 

} 

請注意,在對parseDouble的調用周圍有一個try/catch包裝。如果用戶沒有輸入小時數,或者他們沒有選擇任何設備,這將從方法返回(沒有錯誤)。

最後,ApplianceChangeListener:

public class ApplianceChangeListener implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent ae) { 
     int item = appCB.getSelectedIndex(); 

     if (item == 0) powTF.setText(""); 
     else powTF.setText("" + appPower[item]); 

     calculate(); 
    } 
} 
+0

我遵循你所說的話,它的工作就像一個魅力:D – 2014-12-02 14:54:58

+0

如果你不介意,你能告訴我我應該把重置按鈕嗎?我添加了新的KwH();它只是調用另一個JFrame。我想要的是重置所有的值。 – 2014-12-02 14:56:39

+0

@PatrickLouisFerrer在您的ResetButtonHandler中,將所有字段設置爲其重置值。試試看,讓我們知道你遇到了什麼困難。 – Chris 2014-12-02 15:07:31