Computer Science, asked by sandy772, 1 year ago

design a GUI desktop application in Java to accept the name and favorite sport in two fields and display appropriate message including the name and favorite sport in a dialog box using the concert method. the application must have an exit button to end the application and appropriate labels


anuj: u mean concat method??

Answers

Answered by anuj
6
Hey friend!!

Its a good question 


 So before answering  this i want to explain some terms :
1)
concat() - Its a function used to concat two strings 

Syntax 


String S3=String1.concat(String2);

2)
For displaying in a dialog box we  import option pane class

by Following  :

                      import.javax.swing.jOptionpane;

[its neccasary otherwise the program wil show error]

Now answering the question-:
 First of all design a frame like in the image 1
 
So your frame must have above:-

1) jButton1    // for getting output
1.5) jButton2 // for exit
2)JLabel1
3)JLabel2
4)JTextField1
5)JTextField2



on clicking  jButton1 we have to display the output in a dialog box so follow the  code given below-;

String s1,s2,s3;             //declaring variables for use
s1=JTextField1.getText();  //storing the values 
s2=JTextField2.getText();   // storing thr values
s3=s1.concat(" plays ").concat(s2); // for adding to strings


JOptionPane.showMessageDialog(null,s3);


on clicking the  (jButton2)exit button we have to exit the window so follow the code below

System.exit(0);


DONE!!

  RUN the program(Shift + F6)

 On running your program you will get output like given
             
           in the 3rd image i.e in a dialog box




CAUTIONS:-

1)USE " " for strings,NEVER use ' ' ......it will give you syntax error!!!

2)Don't forget to write  
import.javax.swing.jOptionpane;   at the beginning of the program

Tips/Tricks :

1) we can also use '+' operator instead of using concat operator if it is not given in the question.

2)We can use more concat in one line
like s1.concat(" plays").concat(s2).concat("very well");

this will show 

Rohit plays cricket very well. [input as in images].


These types of questions are easy when one understands the way to do it .


thank you 

HOPE IT HELPS!!!!




Attachments:
Similar questions