Results 1 to 5 of 5
  1. #1
    dbasenoob is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default Java Swing Simple Addition help

    I'm using netbeans then i design 3 jTextField and 1 jButton.

    the jTextField1 and jTextField2 are inputting numbers. and last the jTextField3 is the result sum of the two inputted numbers.

    When i click the jButton should be show the sum of two inputted numbers in the jTextField3.

    So the problem is how could i code this?

    Im new at java swing.

    do you have sample code for this so that i can study it.

    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    dbasenoob is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    
    public class SimpleAdd {
    	public static void main(String[]args)throws IOException {
    		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    		
    		System.out.print("Enter First Number: ");
    		int num1 = Integer.parseInt(input.readLine());
    		System.out.print("Enter Second Number: ");
    		int num2 = Integer.parseInt(input.readLine());
    		
    		int sum = num1+num2;
    			
    		System.out.println("The Sum of Two Numbers is: "+sum);
    	}
    }
    i want to convert this into a gui type. which i want to happened

  4. #4
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    I will give you a simple example of how we can activate the button.

    In the next code, if the user enters his/her name in the textField, the name will be shown in the label.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Print extends JApplet implements ActionListener{
    	private JTextField textField;
    	private JLabel label;
    	private JButton button;
    	
    	public void init(){
    		textField = new JTextField(10);
    		label = new JLabel("Your name");
    		button = new JButton("Enter");
    		
    		button.addActionListener(this);
    			
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new FlowLayout());
    		contentPane.add(textField);
    		contentPane.add(label);
    		contentPane.add(button);
    	}
    	
    	public void actionPerformed(ActionEvent eve){
    		label.setText(textField.getText());
    	}
    }
    Try to figure out how I could make the button works !

  5. #5
    dbasenoob is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. java binary addition
    By rockdude in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 04:01 PM
  2. Addition java program
    By tabrez_k81 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 11:08 AM
  3. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 02:46 PM
  4. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 04:37 AM

Posting Permissions

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