有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我的输出一直显示为空?

为什么我不能计算并将输出设置为创建的textfield?我是这方面的初学者,如果可以的话,请解释清楚。我想将双精度a、b和c计算的输出设置为显示在textfield中,但我不知道我的代码有什么问题

谢谢!谢谢你的帮助

public class calcSalary extends JFrame implements ActionListener
{
Double salary;
    String salary1 = String.valueOf(salary);

    //DecimalFormat currency = new DecimalFormat("0.00");        

Panel p1 = new Panel(new FlowLayout());

    JTextArea t1 = new JTextArea(salary1);        

//Label l1 = new Label("Calculate Staff Salary");
Label l2 = new Label("Hour(s)");
Label l3 = new Label("Day(s)");
Label l4 = new Label("Rate");
Label l5 = new Label("Salary");

Button b1 = new Button("Calculate");
Button b2 = new Button("Back");

Choice c1 = new Choice();
Choice c2 = new Choice();
Choice c3 = new Choice();


public calcSalary()
{
        setLayout(new GridLayout(5,3));

    c1.add("1");//set hours
    c1.add("2");
    c1.add("3");
    c1.add("4");
    c1.add("5");
    c1.add("6");
    c1.add("7");
    c1.add("8");
    c1.add("9");
    c1.add("10");

    c2.add("1");//set days
    c2.add("2");
    c2.add("3");
    c2.add("4");
    c2.add("5");
    c2.add("6");
    c2.add("7");
    c2.add("8");
    c2.add("9");
    c2.add("10");
    c2.add("11");
    c2.add("12");
    c2.add("13");
    c2.add("14");
    c2.add("15");
    c2.add("16");
    c2.add("17");
    c2.add("18");
    c2.add("19");
    c2.add("20");
    c2.add("21");
    c2.add("22");
    c2.add("23");
    c2.add("24");
    c2.add("25");
    c2.add("26");
    c2.add("27");
    c2.add("28");
    c2.add("29");
    c2.add("30");
    c2.add("31");

    c3.add("10");//set rates
    c3.add("15");
    c3.add("20");

    //add(l1);
    add(l2);
    add(c1);
    add(l3);
    add(c2);
    add(l4);
    add(c3);
    add(l5);
            add(t1);
            (t1).setEditable(false);
            add(b1);
    add(b2);

    b1.addActionListener(this);
    b2.addActionListener(this);

}

    /*public boolean check1(String id)
{
    if(id.equals(staffid))
    return true;
    else
    return false;
}*/


/*public void calc()
{
    Double salary;

    int a = c1.getSelectedIndex(); //c1 is the hours
    int b = c2.getSelectedIndex(); //c2 is the days
    double c = c3.getSelectedIndex(); //c3 is the rate

    salary = a * b * c;

}*/


public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == b1)
    {     
                double a = Double.valueOf((String)c1.getSelectedItem());
                //int a = c1.getSelectedIndex(); //c1 is the hours
                double b = Double.valueOf((String)c2.getSelectedItem());
                //int b = c2.getSelectedIndex(); //c2 is the days
                double c = Double.valueOf((String)c3.getSelectedItem());
                //double c = c3.getSelectedIndex(); //c3 is the rate

                salary = a * b * c;
        //TO DO.....
                t1.setText(salary1);
    }

    if(e.getSource() == b2) 
    {
        JFrame frame = new admin();
        frame.setVisible(true);
        setVisible(false);
    }   
}
}   enter code here

共 (1) 个答案

  1. # 1 楼答案

    可能是因为这个原因:

     salary = a * b * c;
     //TO DO.....
     t1.setText(salary1);
    

    您设置了不同的计算变量