0% found this document useful (0 votes)
33 views48 pages

13 Swing 1

This document contains the code for a Java GUI program that performs basic arithmetic operations. The program allows the user to enter two numbers in text fields and select an operation (+, -, *, /, %) using radio buttons. Clicking the "Calculate" button displays the result in a label. The code includes initialization of GUI components like labels, text fields, radio buttons, and buttons. Event handling code is provided to calculate and display the result when the button is clicked.

Uploaded by

Payal Kori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views48 pages

13 Swing 1

This document contains the code for a Java GUI program that performs basic arithmetic operations. The program allows the user to enter two numbers in text fields and select an operation (+, -, *, /, %) using radio buttons. Clicking the "Calculate" button displays the result in a label. The code includes initialization of GUI components like labels, text fields, radio buttons, and buttons. Event handling code is provided to calculate and display the result when the button is clicked.

Uploaded by

Payal Kori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST

PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

Practical
SWING PROGRAMMING
1. Write a java GUI program to perform arithmetic operations.
PROGRAM :
package com.mycompany.swings_program;
public class Arithmetic_Operation extends javax.swing.JFrame {
public Arithmetic_Operation() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Num 1 : ");
jLabel2.setText("Num 2 : ");
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("+");
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("-");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("*");
buttonGroup1.add(jRadioButton4);
jRadioButton4.setText("/");
jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton5);
jRadioButton5.setText("%");
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

Page 1 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField2))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton4))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 57,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton5)))
.addContainerGap(152, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addContainerGap(82, Short.MAX_VALUE))
);

Page 2 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num1 = Integer.parseInt(jTextField1.getText());
int num2 = Integer.parseInt(jTextField2.getText());
if(jRadioButton1.isSelected())
jLabel3.setText("Result : " + (num1+num2));
else if(jRadioButton2.isSelected())
jLabel3.setText("Result : " + (num1-num2));
else if(jRadioButton3.isSelected())
jLabel3.setText("Result : " + (num1*num2));
else if(jRadioButton4.isSelected())
jLabel3.setText("Result : " + (num1/num2));
else if(jRadioButton5.isSelected())
jLabel3.setText("Result : " + (num1%num2));
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Arithmetic_Operation().setVisible(true);
}
});
}
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;

Page 3 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

private javax.swing.JRadioButton jRadioButton5;


private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
OUTPUT :

2. Design Login application program.


PROGRAM :
package com.mycompany.swings_program;
import javax.swing.JOptionPane;
public class Login extends javax.swing.JFrame {
public Login() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("UserID : ");
jLabel2.setText("Password : ");
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)

Page 4 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

.addGap(18, 18, 18)


.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jTextField2))))
.addContainerGap(177, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addComponent(jButton1)
.addContainerGap(140, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String userID = jTextField1.getText();
String password = jTextField2.getText();
if(userID.equals("Admin@123") && password.equals("12345678")) {
JOptionPane.showMessageDialog(rootPane, "Successfully Logged In !");
}
else if(!userID.equals("Admin@123")){
JOptionPane.showMessageDialog(rootPane, "UserID doesn't exist");
}
else{
JOptionPane.showMessageDialog(rootPane, "Invalid Password");
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

Page 5 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
}
OUTPUT :

3. Write a program to accept the temperature in Celsius and display Fahrenheit.


PROGRAM :
package com.mycompany.swings_program;
public class Temperature extends javax.swing.JFrame {
public Temperature() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Enter Temperature in Celsius : ");
jLabel2.setText("°c");
jButton1.setText("Convert");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

Page 6 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(97, 97, 97)
.addComponent(jLabel3)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(87, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addContainerGap(161, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double cel = Double.parseDouble(jTextField1.getText());
double con = (cel*(9/5))+32;
jLabel3.setText("Temperature in Fehrenheit : " + con + "°F");
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Temperature.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {

Page 7 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

java.util.logging.Logger.getLogger(Temperature.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Temperature.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Temperature.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Temperature().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
}
OUTPUT :

4. Write a Java program to obtain the following


output
PROGRAM :
package com.mycompany.swings_program;
public class Length extends javax.swing.JFrame {
public Length() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Page 8 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

jButton1.setText("calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("feet");
jLabel2.setText("is equivalent to");
jLabel3.setText("meters");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 59,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(115, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(131, 131, 131))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addGap(168, 168, 168))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double feet = Double.parseDouble(jTextField1.getText());
double meter = feet*0.3048;

Page 9 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

jLabel4.setText("" + meter);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Length.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Length.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Length.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Length.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Length().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
}
OUTPUT :

5. Write a program to accept information of


customer for an investment policy such as
Name, Date of Birth and Address. The amount to be invested should be selected from a
combo box. A customer can invest from min. age of 21 to 60. According to Date of birth,

Page 10 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

determine current age of customer, total number of years remaining and accordingly find
& display the premium that customer will have to pay.
PROGRAM :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class InvestmentPolicyApp {


private JFrame frame;
private JTextField nameTextField;
private JTextField dobTextField;
private JComboBox<String> investmentAmountComboBox;
private JLabel premiumLabel;

public InvestmentPolicyApp() {
frame = new JFrame("Investment Policy Calculator");
frame.setLayout(new FlowLayout());

JLabel nameLabel = new JLabel("Name:");


JLabel dobLabel = new JLabel("Date of Birth (yyyy-MM-dd):");
JLabel amountLabel = new JLabel("Investment Amount:");
premiumLabel = new JLabel("Premium: ");

nameTextField = new JTextField(20);


dobTextField = new JTextField(10);
investmentAmountComboBox = new JComboBox<>(new String[]{"Select Amount", "10000",
"20000", "30000", "50000", "100000"});

JButton calculateButton = new JButton("Calculate Premium");

frame.add(nameLabel);
frame.add(nameTextField);
frame.add(dobLabel);
frame.add(dobTextField);
frame.add(amountLabel);
frame.add(investmentAmountComboBox);
frame.add(calculateButton);
frame.add(premiumLabel);

calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculatePremium();
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}

private void calculatePremium() {


String name = nameTextField.getText();

Page 11 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

String dobText = dobTextField.getText();


String selectedAmount = (String) investmentAmountComboBox.getSelectedItem();

if (name.isEmpty() || dobText.isEmpty() || selectedAmount.equals("Select Amount")) {


JOptionPane.showMessageDialog(frame, "Please fill in all fields and select an investment
amount.");
return;
}

try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dob = sdf.parse(dobText);
Calendar dobCalendar = Calendar.getInstance();
dobCalendar.setTime(dob);

int age = calculateAge(dobCalendar);

if (age < 21 || age > 60) {


JOptionPane.showMessageDialog(frame, "Customer age must be between 21 and 60.");
return;
}

int selectedAmountValue = Integer.parseInt(selectedAmount);


int premium = calculatePremium(age, selectedAmountValue);

premiumLabel.setText("Premium: $" + premium);


} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Invalid date format. Please use yyyy-MM-dd.");
}
}

private int calculateAge(Calendar dob) {


Calendar currentDate = Calendar.getInstance();
int age = currentDate.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

if (currentDate.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {


age--;
}

return age;
}

private int calculatePremium(int age, int amount) {


int premium;
if (age <= 30) {
premium = (int) (amount * 0.02);
} else if (age <= 45) {
premium = (int) (amount * 0.03);
} else {
premium = (int) (amount * 0.04);
}
return premium;
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
@Override

Page 12 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

public void run() {


new InvestmentPolicyApp();
}
});
}
}
OUTPUT :

6. Write a java Program to create a window as described below: In the window there are
four radio buttons Add, Sub, Multi, div at the top. This group is called Operation. There
are two TextFields to input first and second number. We must type the number and select
one of the operations. On selection of operation the result should be displayed.

PROGRAM :
package com.mycompany.swings_program;
public class Arithmetic_Operation extends javax.swing.JFrame {
public Arithmetic_Operation() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Num 1 : ");
jLabel2.setText("Num 2 : ");
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("+");
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("-");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("*");
buttonGroup1.add(jRadioButton4);

Page 13 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

jRadioButton4.setText("/");
jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton5);
jRadioButton5.setText("%");
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField2))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton4))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 57,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton5)))
.addContainerGap(152, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

Page 14 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addContainerGap(82, Short.MAX_VALUE))
);

pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num1 = Integer.parseInt(jTextField1.getText());
int num2 = Integer.parseInt(jTextField2.getText());
if(jRadioButton1.isSelected())
jLabel3.setText("Result : " + (num1+num2));
else if(jRadioButton2.isSelected())
jLabel3.setText("Result : " + (num1-num2));
else if(jRadioButton3.isSelected())
jLabel3.setText("Result : " + (num1*num2));
else if(jRadioButton4.isSelected())
jLabel3.setText("Result : " + (num1/num2));
else if(jRadioButton5.isSelected())
jLabel3.setText("Result : " + (num1%num2));
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

Page 15 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

java.util.logging.Logger.getLogger(Arithmetic_Operation.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Arithmetic_Operation().setVisible(true);
}
});
}
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
OUTPUT :

7. Write a java program to display the following GUI. On submission, all data should be displayed in
message box.

PROGRAM :
import javax.swing.JOptionPane;
public class MessageBoxGUI extends javax.swing.JFrame {
public MessageBoxGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();

Page 16 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

jScrollPane1 = new javax.swing.JScrollPane();


jTextArea1 = new javax.swing.JTextArea();
jLabel4 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jLabel6 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox<>();
jLabel7 = new javax.swing.JLabel();
jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
jCheckBox3 = new javax.swing.JCheckBox();
jCheckBox4 = new javax.swing.JCheckBox();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name : ");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel2.setText("Age : ");
jLabel3.setText("Address : ");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel4.setText("Phone No : ");
jLabel5.setText("Gender : ");
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("F");
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("M");
jLabel6.setText("Class : ");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "BSC.CS",
"BSC.IT", "B.TECH", "BCA" }));
jLabel7.setText("Interpreted Language");
jCheckBox1.setText("C++");
jCheckBox2.setText("C");
jCheckBox3.setText("Java");
jCheckBox4.setText("Python");
jButton1.setText("Submit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(111, 111, 111)
.addComponent(jLabel7))
.addGroup(layout.createSequentialGroup()

Page 17 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

.addGap(99, 99, 99)


.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jCheckBox3)
.addGap(18, 18, 18)
.addComponent(jCheckBox4))
.addGroup(layout.createSequentialGroup()
.addComponent(jCheckBox1)
.addGap(18, 18, 18)
.addComponent(jCheckBox2))
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jButton1))))
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(30, 30, 30)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField3)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jRadioButton2)))
.addGap(0, 0, Short.MAX_VALUE)))))))
.addContainerGap(53, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

Page 18 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jCheckBox3)
.addComponent(jCheckBox4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap(26, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = jTextField1.getText();
int age = Integer.parseInt(jTextField2.getText());
String address = jTextArea1.getText();
String phoneNo = jTextField3.getText();
String gender = "";
if(jRadioButton1.isSelected())
gender = "Female";
else if(jRadioButton2.isSelected())

Page 19 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

gender = "Male";
String C = jComboBox1.getSelectedItem().toString();
String[] sub = {"", "", "", ""};
if(jCheckBox1.isSelected())
sub[0] = "C++";
if(jCheckBox2.isSelected())
sub[1] = "C";
if(jCheckBox3.isSelected())
sub[2] = "Java";
if(jCheckBox4.isSelected())
sub[3] = "Python";
String msg = "Name : " + name + "\nAge : " + age + "\nAddress : " + address + "\nGender : " +
gender + "\nPhone No : " + phoneNo
+ "\nClass : " + C + "\nSubjects : " + sub[0] + "" + sub[1] + "" + sub[2] + "" + sub[3];
JOptionPane.showMessageDialog(rootPane, msg);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(MessageBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(MessageBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(MessageBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(MessageBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MessageBoxGUI().setVisible(true);
}
});
}
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JCheckBox jCheckBox3;
private javax.swing.JCheckBox jCheckBox4;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;

Page 20 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

private javax.swing.JLabel jLabel3;


private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
}
OUTPUT :

8.
PROGRAM :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SalaryCalculatorApp {


private JFrame frame;
private JTextField empNoTextField;
private JTextField basicPayTextField;
private JTextField deptTextField;

public SalaryCalculatorApp() {
frame = new JFrame("Salary Calculator");
frame.setLayout(new FlowLayout());

JLabel empNoLabel = new JLabel("Employee Number:");


JLabel basicPayLabel = new JLabel("Basic Pay:");
JLabel deptLabel = new JLabel("Department:");

empNoTextField = new JTextField(10);


basicPayTextField = new JTextField(10);

Page 21 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

deptTextField = new JTextField(10);

JButton calculateButton = new JButton("Calculate Salary");

frame.add(empNoLabel);
frame.add(empNoTextField);
frame.add(basicPayLabel);
frame.add(basicPayTextField);
frame.add(deptLabel);
frame.add(deptTextField);
frame.add(calculateButton);

calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateSalary();
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}

private void calculateSalary() {


try {
int empNo = Integer.parseInt(empNoTextField.getText());
double basicPay = Double.parseDouble(basicPayTextField.getText());

double da, hra, cca;

if (basicPay < 5000) {


da = 0.81 * basicPay;
} else if (basicPay >= 5000 && basicPay <= 7000) {
da = 0.51 * basicPay;
} else {
da = 0.41 * basicPay;
}

hra = 0.15 * basicPay;


cca = 350.0;

double totalSalary = basicPay + da + hra + cca;

String message = "Employee Number: " + empNo + "\n";


message += "Basic Pay: $" + basicPay + "\n";
message += "DA: $" + da + "\n";
message += "HRA: $" + hra + "\n";
message += "CCA: $" + cca + "\n";
message += "Total Salary: $" + totalSalary;

JOptionPane.showMessageDialog(frame, message, "Salary Slip",


JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Invalid input. Please enter numeric values for
Employee Number and Basic Pay.");
}

Page 22 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SalaryCalculatorApp();
}
});
}
}
OUTPUT :

9. Write a java program to display the following GUI.


PROGRAM :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ColorChangerApp {


private JFrame frame;
private JButton setBackgroundColorButton;
private JButton setFontColorButton;
private JTextField colorTextField;

public ColorChangerApp() {
frame = new JFrame("Color Changer");
frame.setLayout(new FlowLayout());

setBackgroundColorButton = new JButton("Set Background Color");


setFontColorButton = new JButton("Set Font Color");
colorTextField = new JTextField(15);

frame.add(setBackgroundColorButton);
frame.add(setFontColorButton);
frame.add(colorTextField);

setBackgroundColorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String colorName = colorTextField.getText().toLowerCase();
Color bgColor = getColorByName(colorName);
frame.getContentPane().setBackground(bgColor);
}
});

setFontColorButton.addActionListener(new ActionListener() {
@Override

Page 23 of 24
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :

Aim:

Teacher’s Signature :
SYCS13 Introduction to Java Programming Date-

public void actionPerformed(ActionEvent e) {


String colorName = colorTextField.getText().toLowerCase();
Color fontColor = getColorByName(colorName);
colorTextField.setForeground(fontColor);
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setVisible(true);
}

private Color getColorByName(String colorName) {


switch (colorName) {
case "red":
return Color.RED;
case "blue":
return Color.BLUE;
case "green":
return Color.GREEN;
case "yellow":
return Color.YELLOW;
case "black":
return Color.BLACK;
case "white":
return Color.WHITE;
default:
return Color.GRAY; // Default to gray for unknown color names
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ColorChangerApp();
}
});
}
}

OUTPUT :

Page 24 of 24

You might also like