How to make java program of swapping two numbers in jFrame without using third variable ?

All the college students are welcome. Get connect with other students. Help each other.
Post Reply


Kapil
Posts: 1
Joined: Sat Feb 11, 2017 2:57 pm

How to make java program of swapping two numbers in jFrame without using third variable ?

Post by Kapil » Tue Feb 14, 2017 3:01 pm

Following are the steps that you have to follow to make a program of swapping two nos in NetBeans IDE using JFrame:-
Step 1: Open netbeans project. Then right click on the project name ->new-> JFrame
After that a window will be opened up consisting Design and Source Editor.
Step 2: In design window , make the design of GUI screen by dragging two textfields(for input two nos) and two labels( named first no and second no) and a button(named SWAP to perform swap action)from Pallete window.
Step 3: Now comes on code part
Right click on SWAP button->Events->Action->actionPerformed[jButton1ActionPerformed]
Then source editor will be opened up.
Now you have to type a just simple code shown below to perform swapping

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int x = Integer.parseInt(jTextField1.getText()); // storing 1st no in variable " x " by retrieving from 1st textfield
int y = Integer.parseInt(jTextField2.getText()); // storing 2nd no in variable "y" by retrieving from 2nd textfield

// below lines of code will swap the two nos
jTextField1.setText(""+y);
jTextField2.setText(""+x);

}

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest