Java program, get input from a textfield, button activate actionlistener display total on a label

Опубликовано: 25 Октябрь 2024
на канале: RioProfessor Liu
172
3

JTextField tf = new JTextField(20);

Retrieve value from a textfield,
String strNum = tf.getText();

Convert a String to a number
int num = Integer.ParseInt(tf.getText());

int population = Integer.parseInt(popultaion);
// population is a string retrieved from a html file

double value = Double.parseDouble(tf.getText());

Covert numbers to a string
int i;
double d;
String s3 = Integer.toString(i);
String s4 = Double.toString(d);
Set Text on a label
JLabel label_2 = new JLabel();
label_2.setText(s3);
label_2.setText(Double.toString(total));

Declare a static variable as a global variable to accumulate total amount
private static double total =0;