Results 1 to 6 of 6
  1. #1
    tabrez_k81 is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default Addition java program

    Hello,
    Please help me execute the below program. Integer value is not excepted by setText method.

    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Addition extends JFrame implements ActionListener
    {
    int num1, num2, total;
    JButton b=new JButton("Submit");
    JButton exit=new JButton("Exit");
    JTextField t1=new JTextField(10);
    JTextField t2=new JTextField(10);
    JTextField tsum=new JTextField(10);
    JLabel l1= new JLabel("Number 1");
    JLabel l2= new JLabel("Number 2");
    JLabel sum=new JLabel("Sum");
    public Addition()
    {
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    c.add(l1);
    c.add(t1);
    c.add(l2);
    c.add(t2);
    c.add(sum);
    c.add(tsum);
    c.add(b);
    c.add(exit);
    exit.addActionListener(this);
    try
    {
    num1=Integer.parseInt(t1.getText());
    num2=Integer.parseInt(t2.getText());
    total=num1+num2;
    }
    catch (NumberFormatException e)
    {
    Toolkit.getDefaultToolkit().beep();
    }
    setSize(200,200);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent ae)
    {
    String str=ae.getActionCommand();
    if("Exit".equals(str))
    {
    System.exit(0);
    }
    if("Submit".equals(str))
    {
    tsum.setText(total); // Here is the issue//
    }
    }

  2. #2
    tabrez_k81 is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default Please ignore above program as that is pasted incorrectly.

    Please ignore above program as that is pasted incorrectly.
    Help me execute the below Addition Gui program

    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Addition extends JFrame implements ActionListener
    {
    int num1, num2, total;
    JButton b=new JButton("Submit");
    JButton exit=new JButton("Exit");
    JTextField t1=new JTextField(10);
    JTextField t2=new JTextField(10);
    JTextField tsum=new JTextField(10);
    JLabel l1= new JLabel("Number 1");
    JLabel l2= new JLabel("Number 2");
    JLabel sum=new JLabel("Sum");
    public Addition()
    {
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    c.add(l1);
    c.add(t1);
    c.add(l2);
    c.add(t2);
    c.add(sum);
    c.add(tsum);
    c.add(b);
    c.add(exit);
    exit.addActionListener(this);
    try
    {
    num1=Integer.parseInt(t1.getText());
    num2=Integer.parseInt(t2.getText());
    total=num1+num2;
    }
    catch (NumberFormatException e)
    {
    Toolkit.getDefaultToolkit().beep();
    }
    setSize(200,200);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent ae)
    {
    String str=ae.getActionCommand();
    if("Exit".equals(str))
    {
    System.exit(0);
    }
    if("Submit".equals(str))
    {
    tsum.setText(total);
    }
    }

    public static void main(String args[])
    {
    Addition add=new Addition();
    }
    }

  3. #3
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Try using code tags 'code' and '/code' between square brackets [ ]

  4. #4
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    setText is a method of JTextComponent that accepts a String, not an int. Therefore, you have to convert 'total' to a String. Normally, you would use the toString() method, but because a primitive type doesn't have this, you should use something like tsum.setText("" + total), or better, the static toString method from Integer: tsum.setText(Integer.toString(total)).

  5. #5
    tabrez_k81 is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    Thank you sevn, the program compiles using tsum.setText(""+total) or tsum.setText(Integer.toString(total)); however in the application when I click on submit after entering number 1 and number 2 the sum field is empty.

  6. #6
    tabrez_k81 is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    Thank you Sven, the program compiles using tsum.setText(""+total) or tsum.setText(Integer.toString(total)); however in the application when I click on submit after entering number 1 and number 2 the sum field is empty.

Similar Threads

  1. string addition??
    By j2vdk in forum New To Java
    Replies: 2
    Last Post: 09-03-2008, 03:41 PM
  2. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM
  3. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 05:34 AM
  4. Replies: 0
    Last Post: 03-28-2008, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •