
design a gui application for me and write its code in which the user enters a no. in a textfield and onn clicking the button the sum of the digits of the no. should be displayed. hint: suppose the user enters 12 the output should be 6(1+2+3).

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SumOfDigits{
public static void main(String[] args){
Frame f=new Frame();
Label label1=new Label("Enter Number: ");
Label label2=new Label("Sum of Digits: ");
final TextField text1=new TextField(20);
final TextField text2=new TextField(20);
Button b=new Button("Find");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String v1=text1.getText();
int sum=0;
int n = Integer.parseInt(v1);
int t = n;
while (n > 0) {
int p = n % 10;
sum = sum + p;
n = n / 10;
}
text2.setText(Integer.toString(sum));
}
});
Panel p=new Panel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.