frank_jarle Skrevet 3. september 2003 Del Skrevet 3. september 2003 Jeg holder paa aa lage en applet som skal summere 2 tall nemlig, Item Price og Total: Item Price faar verdin sin fra et textfield og Total=0 Naar jeg kjoerer appleten for forste gang saa virker alt til aa se fint ut, men naar jeg skal summere det andre tallet saa overskriver det verdien til Total. Foerste tall: Item Price: 50 [trykker Enter] | |/ Total: 50 Andre tall: Item Price: 25 [trykker enter] | |/ Total: 25 Egentlig saa skulle Totalt naa ha vert 75. JEg har folgende kode Total = Total + Price; Men det vil bare ikke virke... *********code start********** import java.awt.* ; import java.applet.Applet; import java.awt.event.*; public class C3 extends Applet implements ActionListener{ TextField f; //TextField f2; TextField f3; public void init () { //using this for java applet // Here we build a user interface Panel p; Label l; //Label l2; Label l3; p = new Panel (); p.setLayout (new GridLayout (3,2)); p.setBackground(Color.blue); l = new Label ("Item Price"); //l.setForeground(Color.green); //Setting labeltext to green l.setForeground(Color.white); //setting field text to white //l.setFont (new Font ("Serif",Font.BOLD,20)); //l2 = new Label ("Price-2"); //l2.setForeground(Color.white); //setting field text to white l3 = new Label ("Total Price"); //3.setForeground(new Color(56,106,100)); l3.setForeground(Color.red); //setting field text to white l3.setFont (new Font ("Serif",Font.BOLD,20)); f = new TextField (15); //f.setFont (new Font ("Serif", Font.BOLD,20)); f.setBackground(Color.black); f.setForeground(Color.white); //Setting text color to green //f2 = new TextField (15); //f2.setBackground(Color.black); //setting field color to black //f2.setForeground(Color.white); //setting field text to white f3 = new TextField (20); f3.setFont (new Font ("Serif", Font.BOLD,20)); f3.setBackground(Color.white); //setting field color to black f3.setForeground(Color.black); //setting field text to white Button b1; Button b2; b1 = new Button("Total"); b2 = new Button ("Clear"); b1.addActionListener(this); b2.addActionListener(this); //adding the textfield and label p.add(l); p.add(f); //p.add(l2); //p.add(f2); p.add(l3); p.add(f3); p.add(b1); p.add(b2); add (p); // pack (); This is redundant when using a browser // setVisible(true); This is reduntant when using with a browser } // End of init method public void actionPerformed(ActionEvent e){ String bLabel= e.getActionCommand(); String s; String s2; double price = 0; double price2 = 0; double total = 0; String total2 = new String(); //Here we do the calculation if(bLabel.equals("Total")){ //Value of the first textfield s = f.getText(); //Convert s to double price = Double.parseDouble(s); //Value of the second textfield //s2 = f2.getText(); //Convert s2 to double //price2 = Double.parseDouble(s2); //s.length() ==0 //Add the two numbers together total = price +total; //totalt2 = String.valueOf(total); //total2 = new Double(total).toString(); //Convert the total value to a string total2 = Double.toString(total); //print the string value to the screen f3.setText(total2); f.setText(""); //input thr information you got form the the first textfield to the second text field //f2.setText(new Double(price.getValue()).toString()); } if(bLabel.equals("Clear")){ f.setText(""); //f2.setText(""); f3.setText(""); } } // public static void main (String args[]) { // new C(); // } } *************code end********* JEg er sikker paa at det er simpelt aa fikse programmet, men jeg skjonner ikke bare hva jeg gjor galt? Frankie Singapore Lenke til kommentar
edwood Skrevet 3. september 2003 Del Skrevet 3. september 2003 public void actionPerformed(ActionEvent e){ String bLabel= e.getActionCommand(); String s; String s2; double price = 0; double price2 = 0; double total = 0; String total2 = new String(); //Here we do the calculation if(bLabel.equals("Total")){ ...... total får jo verdien 0 hver gang du trykker total. Initialiser denne variabelen utenfor denne rekkevidden så skal det virke. public class C3 extends Applet implements ActionListener{ TextField f; //TextField f2; TextField f3; double total = 0; Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå