Deva Java
Deva Java
// Create checkboxes }
radio1.addActionListener(this);
radio2.addActionListener(this);
checkBox1.addActionListener(this);
checkBox2.addActionListener(this);
add(radio1);
add(radio2);
add(checkBox1);
1
Devansh Chhatre
2) Design an applet/application to create form using Text Field, Text Area, Button and Label.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// Set layout
setLayout(new FlowLayout());
// Add components
add(new JLabel("Name:"));
add(textField);
add(new JLabel("Comments:"));
add(new JScrollPane(textArea));
add(submitButton);
add(resultLabel);
submitButton.addActionListener(this);
2
Devansh Chhatre
3
Devansh Chhatre
4) Develop a program to select multiple languages known to user. (e. g Marathi, Hindi, English,
Sanskrit).
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LanguageSelectionProgram extends JFrame implements ActionListener {
private JCheckBox marathiCheckbox, hindiCheckbox, englishCheckbox, sanskritCheckbox;
private JLabel resultLabel;
public LanguageSelectionProgram() {
setTitle("Language Selection Program");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
marathiCheckbox = new JCheckBox("Marathi");
hindiCheckbox = new JCheckBox("Hindi");
englishCheckbox = new JCheckBox("English");
sanskritCheckbox = new JCheckBox("Sanskrit");
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(this);
resultLabel = new JLabel();
JPanel checkboxPanel = new JPanel(new GridLayout(4, 1));
checkboxPanel.add(marathiCheckbox);
checkboxPanel.add(hindiCheckbox);
checkboxPanel.add(englishCheckbox);
checkboxPanel.add(sanskritCheckbox);
JPanel buttonPanel = new JPanel();
buttonPanel.add(submitButton);
JPanel resultPanel = new JPanel();
resultPanel.add(resultLabel);
setLayout(new BorderLayout());
add(checkboxPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
add(resultPanel, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e) {
StringBuilder selectedLanguages = new StringBuilder("Selected Languages: ");
if (marathiCheckbox.isSelected()) {
selectedLanguages.append("Marathi, ");
}
if (hindiCheckbox.isSelected()) {
selectedLanguages.append("Hindi, ");
}
if (englishCheckbox.isSelected()) {
selectedLanguages.append("English, ");
}
if (sanskritCheckbox.isSelected()) {
selectedLanguages.append("Sanskrit, ");
}
if (selectedLanguages.length() > 0) {
selectedLanguages.delete(selectedLanguages.length() - 2, selectedLanguages.length());
}
resultLabel.setText(selectedLanguages.toString());
}
4
Devansh Chhatre
SwingUtilities.invokeLater(() -> {
LanguageSelectionProgram program = new LanguageSelectionProgram();
program.setVisible(true);
});
}
}
5) Write a program to create three Buttons with Caption OK, RESET and CANCEL.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public ButtonExample() {
setTitle("Button Example");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ButtonExample.this, "OK button clicked!");
}
});
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ButtonExample.this, "RESET button clicked!");
}
});
5
Devansh Chhatre
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ButtonExample.this, "CANCEL button clicked!");
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
buttonPanel.add(resetButton);
buttonPanel.add(cancelButton);
setLayout(new BorderLayout());
add(buttonPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ButtonExample example = new ButtonExample();
example.setVisible(true);
}
});
}
}
6
Devansh Chhatre
Practical No.2
1) Write Java Program to show following output.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public SeasonFormExample() {
setTitle("Season Form");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
listModel.addElement("Summer");
listModel.addElement("Winter");
listModel.addElement("Rain");
seasonList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
submitButton.addActionListener(new ActionListener() {
@Override
if (selectedSeason != null) {
} else {
});
listPanel.add(new JScrollPane(seasonList));
buttonPanel.add(submitButton);
resultPanel.add(resultLabel);
setLayout(new BorderLayout());
add(listPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
add(resultPanel, BorderLayout.NORTH);
SwingUtilities.invokeLater(() -> {
example.setVisible(true);
});
2) Develop an applet/ application using List components to add names of 10 different cities
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public CityListApp() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
8
Devansh Chhatre
setLayout(new FlowLayout()); }
} }
@Override
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
if (!cityName.isEmpty()) {
cityList.add(cityName);
9
Devansh Chhatre
Choice l;
Newspaper1()
setSize(350,300);
setVisible(true);
setLayout(new FlowLayout());
l = new Choice();
l.add("Hindustan Times");
l.add("Times of India");
l.add("Inqalab");
l.add("Lokmat");
add(l);
}}
10
Devansh Chhatre
11
Devansh Chhatre
12
Devansh Chhatre
Practical No.3
1. Write java Program to Demonstrate Grid of 5* 5
import javax.swing.*;
import java.awt.*;
public GridDemo() {
setTitle("Grid Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
add(button);
SwingUtilities.invokeLater(() -> {
demo.setVisible(true);
});
13
Devansh Chhatre
import java.awt.*;
public NumberButtonsDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
add(button);
SwingUtilities.invokeLater(() -> {
demo.setVisible(true);
});
14
Devansh Chhatre
import java.awt.event.*;
import javax.swing.*;
private Frame f;
public MyGridLayout() {
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.addWindowListener(new WindowAdapter() {
System.exit(0);
});
f.setVisible(true);
f.setSize(300, 300);
SwingUtilities.invokeLater(() -> {
new MyGridLayout();
});
}
}
15
Devansh Chhatre
import java.awt.event.*;
public Region() {
f.add(b1, BorderLayout.EAST);
f.add(b2, BorderLayout.WEST);
f.add(b3, BorderLayout.NORTH);
f.add(b4, BorderLayout.SOUTH);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
new Region();
}
}
16
Devansh Chhatre
Practical No 4
Program Code
17
Devansh Chhatre
import java.awt.event.*;
import javax.swing.*;
public Bag() {
this.setLayout(g);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 0;
gbc.gridy = 1;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 3;
this.setSize(300, 300);
this.setVisible(true);
new Bag();
18
Devansh Chhatre
Practical No 5
1) Write a program which creates Menu of different colors and disable menu item for Black
color.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public ColorMenuApp() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
redItem.addActionListener(this);
greenItem.addActionListener(this);
blueItem.addActionListener(this);
blackItem.addActionListener(this);
colorMenu.add(redItem);
colorMenu.add(greenItem);
colorMenu.add(blueItem);
colorMenu.add(blackItem);
menuBar.add(colorMenu);
setJMenuBar(menuBar);
@Override
19
Devansh Chhatre
if ("Black".equals(color)) {
blackItem.setEnabled(false);
} else {
getContentPane().setBackground(Color.getColor(color.toUpperCase()));
blackItem.setEnabled(true);
SwingUtilities.invokeLater(() -> {
app.setVisible(true);
});
20
Devansh Chhatre
21
Devansh Chhatre
22
Devansh Chhatre
Practical No 6
1) Write a program to develop a frame to select the different states of India using JComboBox
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
};
public StateSelectionApp() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stateComboBox.addActionListener(this);
setLayout(new FlowLayout());
add(stateComboBox);
add(selectedStateLabel);
@Override
23
Devansh Chhatre
SwingUtilities.invokeLater(() -> {
app.setVisible(true);
});
import java.awt.*;
public ScrollPaneDemo() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24
Devansh Chhatre
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
SwingUtilities.invokeLater(() -> {
app.setVisible(true);
});
25
Devansh Chhatre
26