Sie sind auf Seite 1von 15

Chapter 4

Design a GUI desktop application in java to accept the name and favourite sport in two text
fields and display an appropriate message including the name and favourite sport in a dialog
box using the concat( ) method. The application must have an exit button to end the
application and appropriate labels.
Code :
import javax.swing.J OptionPane;
private void MessageActionPerformed(java.awt.event.ActionEvent evt) {
String s1=jTextField1.getText();
String s2=jTextField2.getText();
String s3=s1.concat(" is a great ").concat(s2).concat(" palyer");
J OptionPane.showMessageDialog(null,s3);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :


(b) Design a GUI desktop application in java to accept age category using radio buttons and
display an appropriate age based message in a text area on selection of a radio button. The
application must have an exit button to end the application and appropriate labels.
Code :
private void jRadioButton1I temStateChanged(java.awt.event.I temEvent evt) {
if(jRadioButton1.isSelected()==true)
ta1.setText("You are MINOR \nYou CAN'T Vote in our Elections");
}
private void jRadioButton2I temStateChanged(java.awt.event.I temEvent evt) {
if(jRadioButton2.isSelected()==true)
ta1.setText("You are MAJ OR \nYou CAN Vote in our Elections");
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :


(c) Design a GUI desktop application in java to accept weight in Kilograms in a text field and
convert it into grams and milligrams on the click of two separate buttons. Display the result in
a second text field. The application must have an exit button to end the application and
appropriate labels.
Code :
private void GramsActionPerformed(java.awt.event.ActionEvent evt) {
double w=Double.parseDouble(jTextField1.getText());
double w1=w*1000; //1kg =1000 grams
jTextField2.setText(""+w1);
}
private void MilliActionPerformed(java.awt.event.ActionEvent evt) {
double w=Double.parseDouble(jTextField1.getText());
double w1=w*1000000; //1kg =1000000 milli grams
jTextField2.setText(""+w1);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :


(d) Design a GUI desktop application in java to accept temperature in Celsius in a text field
and display temperature in Fahrenheit in another text field on the click of a button. The
application must have an exit button to end the application and appropriate labels.
Code :
private void ConvertActionPerformed(java.awt.event.ActionEvent evt) {
int c=I nteger.parseI nt(jTextField1.getText());
int f=(c*9/5)+32;
jTextField2.setText(""+f);
// Celsius to Fahrenheit (C 9/5) +32 =F , Fahrenheit to Celsius (F - 32) x 5/9 =C

}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :


Chapter 5
(a) Design a GUI desktop application in java to accept the side of a square in a text field and
calculate the area and perimeter of the square. Display the results in two separate text fields.
Add appropriate labels and an exit button to end the application.
Code :
private void CalculateActionPerformed(java.awt.event.ActionEvent evt) {
double s=Double.parseDouble(jTextField1.getText());
double a=s*s;
double p =4 *s;
jTextField2.setText(""+a);
jTextField3.setText(""+p);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output:


(b) Design a GUI desktop application in java to accept marks in 5 subjects in five text fields
and calculate the total and average marks. Display the results in separate text fields, which are
disabled. Add appropriate labels and an exit button to end the application.
Code :
private void CalculateActionPerformed(java.awt.event.ActionEvent evt) {
double m1=Double.parseDouble(jTextField1.getText());
double m2=Double.parseDouble(jTextField2.getText());
double m3=Double.parseDouble(jTextField3.getText());
double m4=Double.parseDouble(jTextField4.getText());
double m5=Double.parseDouble(jTextField5.getText());
double tot=m1+m2+m3+m4+m5;
double avg=tot/5;
jTextField6.setEnabled(false);
jTextField7.setEnabled(false);
jTextField6.setText(""+tot);
jTextField7.setText(""+avg);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :

(c) Design a GUI desktop application in java to accept sales of a company for four quarters in
text fields. Calculate the total yearly sale and display the same in a dialog box. Add
appropriate labels and an exit button to end the application.
Code :
import javax.swing.J OptionPane;
private void CalculateActionPerformed(java.awt.event.ActionEvent evt) {
double s1=Double.parseDouble(jTextField1.getText());
double s2=Double.parseDouble(jTextField2.getText());
double s3=Double.parseDouble(jTextField3.getText());
double s4=Double.parseDouble(jTextField4.getText());
double tot=s1+s2+s3+s4;
J OptionPane.showMessageDialog(null,"Yearly Sales ="+tot);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :

(d) Design a GUI desktop application in java to accept length in kilometers in a text field and
display the converted length in meters in a second text field which is disabled. Add appropriate
labels and an exit button to end the application
Code :
private void ConvertActionPerformed(java.awt.event.ActionEvent evt) {
double len=Double.parseDouble(jTextField1.getText());
double len1=len*1000; //1Km =1000 mtrs
jTextField2.setEnabled(false);
jTextField2.setText(""+len1);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :


(e) Design a GUI desktop application in java to accept two numbers in a in a text field and
I nterchange the values of first number and second number using a temporary variable. Add
appropriate labels and an exit button to end the application.
Code :
private void SwapActionPerformed(java.awt.event.ActionEvent evt) {
int fn=I nteger.parseI nt(jTextField1.getText());
int sn=Integer.parseI nt(jTextField2.getText());
int temp=fn;
fn=sn;
sn=temp;
jTextField3.setText(""+fn);
jTextField4.setText(""+sn);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
Output :


(f) Write the code for the following application

[Hint :Tc =(5/9)*(J TextField-32) and J TextField =(9/5)*Tc+32 where Tc =temperature in
degrees Celsius, J TextField =temperature in degrees Fahrenheit]
Code :
private void ConvertActionPerformed(java.awt.event.ActionEvent evt) {
int c=I nteger.parseI nt(jTextField1.getText());
int f=(c*9/5)+32;
jTextField2.setText(""+f);
}
(g) Write the code for the following application :
[Hint : Area of Rectangle=Length*Breadth and Perimeter of Rectangle =2 * ( Length +
Breadth ) ]


Code :
private void ConvertActionPerformed(java.awt.event.ActionEvent evt) {
int l=I nteger.parseI nt(jTextField1.getText());
int b=I nteger.parseI nt(jTextField2.getText());
jTextField3.setText(""+(l*b)); // Area
jTextField4.setText(""+(2*(l+b))); // Perimeter
}
(h) Write the code for the following application :
[Hint : SI [I nterest] =(PRT)/100 and Amount =Principle +SI ]

Code :
private void ConvertActionPerformed(java.awt.event.ActionEvent evt) {
double p=Double.parseDouble(jTextField1.getText());
double r=Double.parseDouble(jTextField2.getText());
double t=Double.parseDouble(jTextField3.getText());
double si=(p*t*r)/100;
double amt =p+si;
jTextField4.setText(""+si);
jTextField5.setText(""+amt);
}
Chapter 6
(a) Develop an application to take input from user in a radio button out of the two referring to Area or
Perimeter of a circle. Print the Area or Perimeter in a TextField for the value of Radius entered in
another TextField.
Code :
private void jRadioButton1ItemStateChanged(java.awt.event.ItemEvent evt) {
if(jRadioButton1.isSelected()==true)
{
double r=Double.parseDouble(jTextField1.getText());
jTextField2.setText(""+(3.14*r*r));
}
}
private void jRadioButton2ItemStateChanged(java.awt.event.ItemEvent evt) {
if(jRadioButton2.isSelected()==true)
{
double r=Double.parseDouble(jTextField1.getText());
jTextField2.setText(""+(2*3.14*r));
}
}
Output :


(b) Develop an application to take an input in TextField for a number. If the number is even then
Display its square otherwise its cube in a Message Box.
Code :
import javax.swing.JOptionPane;
private void MessageActionPerformed(java.awt.event.ActionEvent evt) {
int n= Integer.parseInt(jTextField1.getText());
if(n%2==0)
JOptionPane.showMessageDialog(null,"Square is "+(n*n));
else
JOptionPane.showMessageDialog(null,"Cube is "+(n*n*n));
}
Output :


(c) Develop an application to calculate area of a circle, a rectangle or a triangle depending upon the
user's choice (from a set of Radio Buttons). Accept the desired input Radius OR Length-Breadth OR
Side as per the option selected by the user.
Code :
import javax.swing.JOptionPane;
private void formWindowActivated(java.awt.event.WindowEvent evt) {
Circle.setVisible(false); // Circle, Rectangle & Triangle are Panels
Rectangle.setVisible(false);
Triangle.setVisible(false);
}
private void jRadioButton1ItemStateChanged(java.awt.event.ItemEvent evt) {
if(jRadioButton1.isSelected()==true)
{
Circle.setVisible(true);
Rectangle.setVisible(false);
Triangle.setVisible(false);
}
}
private void ok1ActionPerformed(java.awt.event.ActionEvent evt) {
double r=Double.parseDouble(jTextField1.getText());
JOptionPane.showMessageDialog(null,"Area of Circle is "+(3.14*r*r));
}
private void jRadioButton2ItemStateChanged(java.awt.event.ItemEvent evt) {
if(jRadioButton2.isSelected()==true)
{
Rectangle.setVisible(true);
Circle.setVisible(false);
Triangle.setVisible(false);
}
}
private void ok2ActionPerformed(java.awt.event.ActionEvent evt) {
double l=Double.parseDouble(jTextField2.getText());
double b=Double.parseDouble(jTextField3.getText());
JOptionPane.showMessageDialog(null,"Area of Rectangle is "+(l*b));
}
private void jRadioButton3ItemStateChanged(java.awt.event.ItemEvent evt) {
if(jRadioButton3.isSelected()==true)
{
Triangle.setVisible(true);
Circle.setVisible(false);
Rectangle.setVisible(false);
}
}
private void ok3ActionPerformed(java.awt.event.ActionEvent evt) {
double b=Double.parseDouble(jTextField4.getText());
double h=Double.parseDouble(jTextField5.getText());
JOptionPane.showMessageDialog(null,"Area of Triangle is "+(0.5*b*h));
}
Output :


(d) An electronic shop has announced the following seasonal discounts on the purchase of certain
items

Develop an application based on the above criteria, to input amount of purchase and the type of
purchase ( TV or Music System using JRadioButton) by a customer. Compute and print the net amount
to be paid by a customer along with his name accepted in a text field.
[Hint: Discount = ( Discount rate / 100) * Amount of purchase Net amount = amount of purchase -
discount).]
Code :
import javax.swing.JOptionPane;
private void ComputeActionPerformed(java.awt.event.ActionEvent evt) {
String name=jTextField1.getText();
double amt=Double.parseDouble(jTextField2.getText());
double disc=0.0;
if(jRadioButton1.isSelected()==true)
{
if(amt>0 && amt<=25000)
disc=amt*0.05;
else if(amt>=25001 && amt<=50000)
disc=amt*0.10;
if(amt>50000)
disc=amt*0.15;
}
else if(jRadioButton2.isSelected()==true)
{
if(amt>0 && amt<=25000)
disc=amt*0.10;
else if(amt>=25001 && amt<=50000)
disc=amt*0.20;
if(amt>50000)
disc=amt*0.30;
}
double net=amt-disc;
JOptionPane.showMessageDialog(null,"Mr./Ms. "+name+" Pay Rs. "+net);
}
Output :

(e) Define a GUI application to create a list box ClassName with the following values.

Write a program to print the names of the class teacher according to the class selected based on the
following information
Code :
private void list1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
String name="";
if(list1.getSelectedIndex()==0)
name="Purnima Singh";
else if(list1.getSelectedIndex()==1)
name="Suruchi Oberoi";
else if(list1.getSelectedIndex()==2)
name="Manjula";
else if(list1.getSelectedIndex()==3)
name="Anita Mishra";
jTextField1.setText(name);
}
Output :


(f) Design a GUI application as shown below: On selecting the radio button and clicking the Set
Alignment button the alignment of the text on the button gets changed to Left, Right or Centre
[Hint use the setHorizontalAlignment method. To set the alignment to right we can use
setHorizontalAlignment (SwingConstants.RIGHT).]

Code :
import javax.swing.SwingConstants;
private void alignActionPerformed(java.awt.event.ActionEvent evt) {
if(jRadioButton1.isSelected()==true)
align.setHorizontalAlignment(SwingConstants.LEFT);
else if(jRadioButton2.isSelected()==true)
align.setHorizontalAlignment(SwingConstants.RIGHT);
else if(jRadioButton3.isSelected()==true)
align.setHorizontalAlignment(SwingConstants.CENTER);
}
Output :

Das könnte Ihnen auch gefallen