[英]Why my output keeps being shown as NULL?
Why can't I calculate and set my output to the textfield
created? 为什么我不能计算输出并将其设置为创建的textfield
? I'm a beginner on this so please explain clearly if you could. 我是对此的初学者,所以请清楚说明。 I would like to set the output calculated by double a, b and c to be display inside the textfield
but I just have no idea what's wrong with my codes. 我想将由双a,b和c计算得出的输出设置为在textfield
内显示,但我不知道我的代码有什么问题。
Thanks! 谢谢! All help appreciated. 所有帮助表示赞赏。
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
Probably because of this: 可能是因为:
salary = a * b * c;
//TO DO.....
t1.setText(salary1);
You set different variable that you've computed. 您设置了您计算出的其他变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.