我正在創建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();
}
}
使用JCOmponents可以返回雙精度值,休息爲(其型號)設置爲從ActionListener的 – mKorbel 2014-12-02 13:39:33
@Patrick路易·費雷爾解析接受雙重價值,無副作用感謝發佈功能代碼。不完全確定你想要什麼。當您更改組合框項目時,是否希望程序自動填充「功耗」?或者你只是想重新計算? – Chris 2014-12-02 14:07:09
@Chris我想要的是,如果用戶選擇一個項目,等效瓦特將自動顯示功耗,例如,如果我選擇冰箱,那麼在功耗上必須出現200瓦的值。那麼用戶將輸入使用小時數,然後計算 – 2014-12-02 14:16:25