简体   繁体   中英

Dynamically Change JTextField?

My purpose here is to have the user click 10 buttons and have it show the sum in the uneditable JTextField. I did a little test in the console to show it was calculating right and to my success it was: The console was adding up the numbers as I clicked them. ( http://i.imgur.com/Cwyjm0h.jpg )

I know that you can't enter an int value into a JTextField so I tried using String.valueOf(); to convert it and properly output it. The console is stating it's at 0 all the time. Maybe it needs to be reinitalized elsewhere? I can't tell. I also don't believe it's changing when I click a button. I assume thats not how it works but I don't know what will so here I am.

So my question is:

1) What am I doing wrong in converting total to sTotal

2) If the number won't change even with an actionevent, What are my alternatives?

Thank you in advance and here is the source!

EDIT: I fixed the issue with the string converting by adding

String sTotal = String.valueOf(total);
    System.out.println(sTotal);

at the bottom under the system.out.println.(total);

import java.awt.FlowLayout;
import java.awt.event.ActionListener;  
import java.awt.event.ActionEvent;
import javax.swing.JFrame; 
import javax.swing.JButton;
import javax.swing.JTextField;

//JFrame builds the window
public class ThreeOne extends JFrame{

int total;
String sTotal = String.valueOf(total);

private JTextField question;
private JButton Button1, Button2, Button3, Button4, Button5, 
Button6, Button7, Button8, Button9, Button10;



//The window
public ThreeOne(){
    super("");
    setLayout(new FlowLayout());

    question = new JTextField(sTotal, 40);

    question.setEditable(false);
    add(question);

    Button1 = new JButton("1");
    add(Button1);
    Button2 = new JButton("2");
    add(Button2);
    Button3 = new JButton("3");
    add(Button3);
    Button4 = new JButton("4");
    add(Button4);
    Button5 = new JButton("5");
    add(Button5);
    Button6 = new JButton("6");
    add(Button6);
    Button7 = new JButton("7");
    add(Button7);
    Button8 = new JButton("8");
    add(Button8);
    Button9 = new JButton("9");
    add(Button9);
    Button10 = new JButton("10");
    add(Button10);





    thehandler handler = new thehandler();
    Button1.addActionListener(handler);
    Button2.addActionListener(handler);
    Button3.addActionListener(handler);
    Button4.addActionListener(handler);
    Button5.addActionListener(handler);
    Button6.addActionListener(handler);
    Button7.addActionListener(handler);
    Button8.addActionListener(handler);
    Button9.addActionListener(handler);
    Button10.addActionListener(handler);


}

//implements means this is the class that handles the events
private class thehandler implements ActionListener{

    public void actionPerformed(ActionEvent event){



        // event is like an enter or a click
        if(event.getSource()== Button1)
        total = total + 1;
        if(event.getSource()== Button2)
            total = total + 2;

        if(event.getSource()== Button3)
            total = total + 3;

        if(event.getSource()== Button4)
            total = total + 4;

        if(event.getSource()== Button5)
            total = total + 5;

        if(event.getSource()== Button6)
            total = total + 6;

        if(event.getSource()== Button7)
            total = total + 7;

        if(event.getSource()== Button8)
            total = total + 8;

        if(event.getSource()== Button9)
            total = total + 9;

        if(event.getSource()== Button10)
            total = total + 10;



    System.out.println(total);




    }

}
      }

You need to update it in your Actionlistener.

Right before your System.out.println

public void actionPerformed(ActionEvent event){

    ....
    question.setText(Integer.toString(total)); //can use "" + total, just need to be a String
    System.out.println(total); //you can use this for sanity checking
}

sTotal doesn't do anything as it is created at instantiation time with a value of 0.

You're passing a String "value" to the application, not a reference to it. sTotal will remain "0" until you update it, and there is no evidence of it being updated except at instantiation. Even if sTotal were updated, the component will not get updated, since the String value was passed and not the reference to the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM