我正在创建GUI程序称为KwH计算器,它的作用是计算某个项目的耗电量,所以我的问题是在组合框中,我想要的是如果用户在选择中选择一个项目瓦特将自动出现功耗(powTF)。
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();
}
}
答案 0 :(得分: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中的所有代码移动到名为calculate的新方法中,然后让按钮处理程序调用计算。
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();
}
}