0

このプログラムでテキストフィールドに費やされた1週間の合計金額を表示するつもりでしたが、金額を変数に設定した後、このテキストボックスを「更新」する方法がわかりません。何か案は?前もって感謝します。

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^^


}



  }
4

3 に答える 3

1

public void actionPerformed(ActionEvent ae) {

あなたは付け加えられます:

finalBox.setText(Double.toString(sum));
于 2012-08-12T14:42:27.287 に答える
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));
    }
});
于 2012-08-12T14:39:54.930 に答える
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));
    }
});
于 2012-08-12T15:16:53.680 に答える