我打算让这个程序显示每周在文本字段中花费的总金额,但我不确定如何在将金额设置为变量后使此文本框“更新”。有任何想法吗?提前致谢。
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^^
}
}