2012-08-12 45 views
0

我打算讓這個程序顯示每週在文本字段中花費的總金額,但我不知道如何在將金額設置爲變量後將此文本框「更新」。有任何想法嗎?提前致謝。如何更新文本框,以便它可以更改金額?

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


} 



    } 

回答

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)); 
    } 
}); 
+0

非常感謝!我不相信我是多麼愚蠢,但對像你這樣的人感激。謝謝! – 2012-08-12 14:41:15

1

public void actionPerformed(ActionEvent ae) { 

您可以添加:

finalBox.setText(Double.toString(sum)); 
1

好,一旦你已經拿到了總金額,你需要把它放在上。

您將需要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)); 
    } 
}); 
相關問題