java - JComboBox 添加值

标签 java swing user-interface jcombobox

我正在创建一个名为 KwH Calculator 的 GUI 程序,它的作用是计算某个项目的用电量,所以我的问题是在组合框中,我想要的是,如果用户在选择中选择一个项目瓦特将自动出现在功耗(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();
}

}

最佳答案

就目前的示例而言,用户仍然需要输入功耗。与您当前的设计保持一致,添加功耗值数组:

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

为了保持“计算”按钮正常工作并添加功能,我建议将所有代码从“计算按钮处理程序”移动到一个名为“计算”的新方法中,然后让按钮处理程序调用“计算”​​。

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

关于java - JComboBox 添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27250662/

相关文章:

python - 使用 Python 下载文件的问题

Java:是否有用于 boolean 值的++ 版本?

java - JAVA动态类加载的性能开销

Java swing 搜索数组列表和表单

java - 为什么我的 JLabel 不能在循环中正确更新?

java - 在饼图切片中间绘制带三角形的饼图

java - 如何让JList显示ArrayList?

android - dp 中指定的边距在不同屏幕上的相对大小不同

java - GreeterEJB 和 GreeterLibrary 之间的依赖关系的本质是什么?

java - 每次页面加载后Xpath都会改变