我打算让这个程序显示每周在文本字段中花费的总金额,但我不确定如何在变量中设置金额后使此文本框“更新”。有任何想法吗?提前谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class operation extends JFrame{
private JTextField monday;
private JTextField tuesday;
private JTextField wednesday;
private JTextField thursday;
private JTextField friday;
private JTextField saturday;
private JTextField sunday;
private JButton finalize;
private JButton result;
private JTextField finalBox;
double mon = 0;
double tues = 0;
double wed = 0;
double thurs = 0;
double fri = 0;
double sat = 0;
double sun = 0;
double sum = 0;
String stringSum = "";
public operation(){
super("Weekly Total Calculator");
setLayout(new FlowLayout());
monday = new JTextField("How much you spent Monday", 20);
monday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(monday);
tuesday = new JTextField("How much you spent Tuesday", 20);
tuesday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(tuesday);
wednesday = new JTextField("How much you spent Wednesday", 20);
wednesday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(wednesday);
thursday = new JTextField("How much you spent Thursday", 20);
thursday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(thursday);
friday = new JTextField("How much you spent Friday", 20);
friday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(friday);
saturday = new JTextField("How much you spent Saturday", 20);
saturday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(saturday);
sunday = new JTextField("How much you spent Sunday", 20);
sunday.setFont(new Font("Ariel", Font.PLAIN, 14));
add(sunday);
finalize = new JButton("Calculate!");
add(finalize);
finalize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
double mon = Double.parseDouble(monday.getText());
double tues = Double.parseDouble(tuesday.getText());
double wed = Double.parseDouble(wednesday.getText());
double thurs = Double.parseDouble(thursday.getText());
double fri = Double.parseDouble(friday.getText());
double sat = Double.parseDouble(saturday.getText());
double sun = Double.parseDouble(sunday.getText());
double sum = mon + tues + wed + thurs + fri + sat + sun;
}
});
String stringSum = Double.toString(sum);
finalBox = new JTextField(stringSum, 20);
finalBox.setEditable(false);
add(finalBox);
//this is the text box^^
}
}
答案 0 :(得分:1)
在动作监听器中设置测试:
finalize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
// snip
// snap
double sum = mon + tues + wed + thurs + fri + sat + sun;
finalBox.setText(Double.toString(sum));
}
});
答案 1 :(得分:1)
在
public void actionPerformed(ActionEvent ae) {
你可以添加:
finalBox.setText(Double.toString(sum));
答案 2 :(得分:1)
好的,一旦你有总金额,你需要把它放在finalBox
。
您将需要setText()
方法,这是一种线程安全的方法。
finalize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
double mon = Double.parseDouble(monday.getText());
double tues = Double.parseDouble(tuesday.getText());
double wed = Double.parseDouble(wednesday.getText());
double thurs = Double.parseDouble(thursday.getText());
double fri = Double.parseDouble(friday.getText());
double sat = Double.parseDouble(saturday.getText());
double sun = Double.parseDouble(sunday.getText());
double sum = mon + tues + wed + thurs + fri + sat + sun;
finalBox.setText(Double.toString(sum));
}
});