25
Feb
2019

How to Add multiple textbox values and showing their sum

How can we add multiple textboxes data and show their result in an other textbox using GUI or using a button.
Use TextField to Input multiple data in Java
Get Values from Multiple textbox
Java GUI use one button to add multiple text fields to a text file
how to insert value in multiple text box in java

import java.awt.; import java.awt.event.;
class kframe extends Frame implements ActionListener
{
Checkbox pizza,pepsi,salad;
Label quantity,total;
TextField q1,q2,q3,t1;
Button calculate;
kframe()
{
super(“Place Your Order”);
setBounds(150,150,320,310);
setBackground(new Color(55,105,155));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
setLayout(null);

pizza=new Checkbox(“PIZZA = 500”);
pizza.setBounds(40,75,130,35);
pizza.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));

salad=new Checkbox(“SALAD = 50”);
salad.setBounds(40,110,130,35);
salad.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));

pepsi=new Checkbox(“PEPSI = 50”);
pepsi.setBounds(40,145,130,35);
pepsi.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));

q1=new TextField(35);
q1.setBounds(180,75,100,25);
q1.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));
q1.setText(“1”);

q2=new TextField(35);
q2.setBounds(180,110,100,25);
q2.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));
q2.setText(“1”);

q3=new TextField(35);
q3.setBounds(180,145,100,25);
q3.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));
q3.setText(“1”);

t1=new TextField(35);
t1.setBounds(180,195,100,25);
t1.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));

quantity=new Label(“QUANTITY “);
quantity.setBounds(180,40,130,25);
quantity.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));

total=new Label(“TOTAL “);
total.setBounds(50,195,100,25);
total.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));

calculate=new Button(“Calculate “);
calculate.setBounds(100,250,100,25);
calculate.setBackground(new Color(55,255,55));
calculate.setFont(new Font(“Monotype Corsiva”,Font.BOLD,20));
calculate.addActionListener(this);

add(quantity);
add(pizza);
add(q1);
add(pepsi);
add(q2);
add(salad);
add(q3);
add(total);
add(t1);
add(calculate);

setVisible(true);
}

adding multiple textbox value and showing their sum.

public void actionPerformed(ActionEvent ae)
{
int t=0,f=0;
String str=” “;
if(ae.getSource()==calculate)
{
if(pizza.getState())
t=t+(500Integer.parseInt(q1.getText())); if(pepsi.getState()) t=t+(50Integer.parseInt(q2.getText()));
if(salad.getState())
t=t+(50*Integer.parseInt(q3.getText()));
t1.setText(str.valueOf(t));
}
}
}
class b4
{
public static void main(String str[])
{
kframe f=new kframe();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *