Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Deva Java

The document describes 5 Java programs: 1) An application demonstrating radio buttons and checkboxes. 2) A form application using text fields, text areas, buttons and labels. 3) A welcome applet displaying the label "Welcome to Java". 4) A program allowing selection of multiple languages using checkboxes. 5) A program creating buttons with captions "OK", "RESET" and "CANCEL".

Uploaded by

desar69234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Deva Java

The document describes 5 Java programs: 1) An application demonstrating radio buttons and checkboxes. 2) A form application using text fields, text areas, buttons and labels. 3) A welcome applet displaying the label "Welcome to Java". 4) A program allowing selection of multiple languages using checkboxes. 5) A program creating buttons with captions "OK", "RESET" and "CANCEL".

Uploaded by

desar69234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Devansh Chhatre

1) Design an applet/application to demonstrate the use of Radio Button and Checkbox.


import javax.swing.*; add(checkBox2);
import java.awt.*;
}
import java.awt.event.ActionEvent; import

java.awt.event.ActionListener; public void actionPerformed(ActionEvent e) {if (e.getSource() ==


public class RadioButtonCheckBoxDemo extends radio1)
{
JFrame implements ActionListener System.out.println("Radio 1 selected");
{
} else if (e.getSource() == radio2)
private JRadioButton radio1, radio2; {
System.out.println("Radio 2 selected");
private JCheckBox checkBox1, checkBox2; }
public RadioButtonCheckBoxDemo() { else if (e.getSource() == checkBox1)
{
setTitle("Radio Button and Checkbox Demo"); System.out.println("Checkbox 1: " + checkBox1.isSelected());
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else if (e.getSource() == checkBox2)
setSize(300, 200); {
System.out.println("Checkbox 2: " + checkBox2.isSelected());
setLocationRelativeTo(null); }
// Create radio buttons }
radio1 = new JRadioButton("Option 1");
public static void main(String[] args) {SwingUtilities.invokeLater(() ->
radio2 = new JRadioButton("Option 2"); {
RadioButtonCheckBoxDemo demo = new
ButtonGroup radioGroup = new ButtonGroup(); RadioButtonCheckBoxDemo();demo.setVisible(true);
radioGroup.add(radio1); radioGroup.add(radio2); });

// Create checkboxes }

checkBox1 = new JCheckBox("Checkbox 1"); }


checkBox2 = new JCheckBox("Checkbox 2");

// Add action listeners

radio1.addActionListener(this);

radio2.addActionListener(this);

checkBox1.addActionListener(this);

checkBox2.addActionListener(this);

// Set layout and add components

setLayout(new GridLayout(4, 1));

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;

public class FormApp extends JFrame implements ActionListener {

private JTextField textField;

private JTextArea textArea; public void actionPerformed(ActionEvent e) {if (e.getSource() ==


submitButton) {
private JButton submitButton;
String name = textField.getText(); String comments =
private JLabel resultLabel; textArea.getText();
resultLabel.setText("Results:\nName: " + name + "\nComments:
public FormApp() {
" + comments);
setTitle("Form Application");
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
setSize(400, 300);
public static void main(String[] args) {
setLocationRelativeTo(null);
SwingUtilities.invokeLater(() -> {
// Create components FormApp app = new FormApp();app.setVisible(true);
});
textField = new JTextField(20);

textArea = new JTextArea(5, 20); }

submitButton = new JButton("Submit"); }

resultLabel = new JLabel("Results:");

// 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);

// Add action listener to the button

submitButton.addActionListener(this);

2
Devansh Chhatre

3) Develop a program using Label to display message “Welcome to Java”


import
java.applet.Applet;
import java.awt.Label;

public class WelcomeApplet


extends Applet { public void
init() {
Label messageLabel = new
Label("Welcome to Java");
add(messageLabel);
}
}

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());
}

public static void main(String[] args) {

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 class ButtonExample extends JFrame {


private JButton okButton, resetButton, cancelButton;

public ButtonExample() {
setTitle("Button Example");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);

okButton = new JButton("OK");


resetButton = new JButton("RESET");
cancelButton = new JButton("CANCEL");

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 class SeasonFormExample extends JFrame {

private JList<String> seasonList;

private JLabel resultLabel;

public SeasonFormExample() {

setTitle("Season Form");

setSize(300, 200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

DefaultListModel<String> listModel = new DefaultListModel<>();

listModel.addElement("Summer");

listModel.addElement("Winter");

listModel.addElement("Rain");

seasonList = new JList<>(listModel);

seasonList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

JButton submitButton = new JButton("Submit");

submitButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String selectedSeason = seasonList.getSelectedValue();

if (selectedSeason != null) {

resultLabel.setText("Selected season: " + selectedSeason);

} else {

resultLabel.setText("Please select a season.");

});

resultLabel = new JLabel();


7
Devansh Chhatre

JPanel listPanel = new JPanel();

listPanel.add(new JScrollPane(seasonList));

JPanel buttonPanel = new JPanel();

buttonPanel.add(submitButton);

JPanel resultPanel = new JPanel();

resultPanel.add(resultLabel);

setLayout(new BorderLayout());

add(listPanel, BorderLayout.CENTER);

add(buttonPanel, BorderLayout.SOUTH);

add(resultPanel, BorderLayout.NORTH);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

SeasonFormExample example = new SeasonFormExample();

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 class CityListApp extends JFrame implements ActionListener {

private List cityList;

private JTextField cityTextField;

private JButton addButton;

public CityListApp() {

setTitle("City List Application");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 200);

setLocationRelativeTo(null);

cityList = new List(10);

cityTextField = new JTextField(15);

8
Devansh Chhatre

addButton = new JButton("Add");


cityTextField.setText("");
// Add action listener to the button
}
addButton.addActionListener(this);

setLayout(new FlowLayout()); }

add(new JLabel("Enter City:")); }


add(cityTextField);
public static void main(String[] args) {
add(addButton); SwingUtilities.invokeLater(() -> {
add(new Label("City List:")); CityListApp app = new CityListApp();app.setVisible(true);
add(cityList);
});

} }
@Override
}
public void actionPerformed(ActionEvent e) {

if (e.getSource() == addButton) {

String cityName = cityTextField.getText();

if (!cityName.isEmpty()) {

cityList.add(cityName);

9
Devansh Chhatre

3) Develop applet / application to select multiple names of news papers


import java.awt.*;

class Newspaper1 extends Frame

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");

l.setBounds(100, 100, 100, 200);

add(l);

public static void main( String[] args )

Newspaper1 f = new Newspaper1();

}}

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 class GridDemo extends JFrame {

public GridDemo() {

setTitle("Grid Demo");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400, 400);

setLocationRelativeTo(null);

setLayout(new GridLayout(5, 5));

for (int i = 1; i <= 25; i++) {

JButton button = new JButton("Button " + i);

add(button);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

GridDemo demo = new GridDemo();

demo.setVisible(true);

});

13
Devansh Chhatre

1. Write a program to display The Number on Button from 0 to 9.


import javax.swing.*;

import java.awt.*;

public class NumberButtonsDemo extends JFrame {

public NumberButtonsDemo() {

setTitle("Number Buttons Demo");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 300);

setLocationRelativeTo(null);

setLayout(new GridLayout(2, 5));

for (int i = 0; i <= 9; i++) {

JButton button = new JButton(Integer.toString(i));

add(button);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

NumberButtonsDemo demo = new NumberButtonsDemo();

demo.setVisible(true);

});

14
Devansh Chhatre

1. Write a program to generate following output


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyGridLayout {

private Frame f;

public MyGridLayout() {

f = new Frame("My Grid Layout Example");

f.setLayout(new GridLayout(2, 2));

Button b1 = new Button("Button 1");

Button b2 = new Button("Button 2");

Button b3 = new Button("Button 3");

Button b4 = new Button("Button 4");

Button b5 = new Button("Button 5");

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

});

f.setVisible(true);

f.setSize(300, 300);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

new MyGridLayout();

});

}
}

15
Devansh Chhatre

1) Write a program to generate following output using Border Layout


import java.awt.*;

import java.awt.event.*;

public class Region {

public Region() {

Frame f = new Frame();

Button b1 = new Button("East");

Button b2 = new Button("West");

Button b3 = new Button("North");

Button b4 = new Button("South");

Button b5 = new Button("Center");

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);

public static void main(String[] args) {

new Region();

}
}

16
Devansh Chhatre

Practical No 4
Program Code

17
Devansh Chhatre

1) Write a program to generate following output


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Bag extends JFrame {

public Bag() {

GridBagLayout g = new GridBagLayout();

GridBagConstraints gbc = new GridBagConstraints();

this.setLayout(g);

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridy = 0;

this.add(new Button("Button One"), gbc);

gbc.gridy = 1;

this.add(new Button("Button Two"), gbc);

gbc.gridy = 2;

this.add(new Button("Button Three"), gbc);

gbc.gridx = 1;

gbc.gridy = 0;

gbc.gridwidth = 2;

this.add(new Button("Button Four"), gbc);

gbc.gridx = 0;

gbc.gridy = 3;

gbc.gridwidth = 3;

this.add(new Button("Button Five"), gbc);

this.setSize(300, 300);

this.setVisible(true);

public static void main(String[] args) {

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 class ColorMenuApp extends JFrame implements ActionListener {

private JMenuBar menuBar;

private JMenu colorMenu;

private JMenuItem redItem, greenItem, blueItem, blackItem;

public ColorMenuApp() {

setTitle("Color Menu App");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

menuBar = new JMenuBar();

colorMenu = new JMenu("Colors");

redItem = new JMenuItem("Red");

greenItem = new JMenuItem("Green");

blueItem = new JMenuItem("Blue");

blackItem = new JMenuItem("Black");

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

public void actionPerformed(ActionEvent e) {

String color = e.getActionCommand();

if ("Black".equals(color)) {

blackItem.setEnabled(false);

} else {

getContentPane().setBackground(Color.getColor(color.toUpperCase()));

blackItem.setEnabled(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

ColorMenuApp app = new ColorMenuApp();

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 class StateSelectionApp extends JFrame implements ActionListener {

private JComboBox<String> stateComboBox;

private JLabel selectedStateLabel;

private String[] states = {

"Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh",

"Goa", "Gujarat", "Haryana", "Himachal Pradesh", "Jharkhand",

"Karnataka", "Kerala", "Madhya Pradesh", "Maharashtra", "Manipur",

"Meghalaya", "Mizoram", "Nagaland", "Odisha", "Punjab",

"Rajasthan", "Sikkim", "Tamil Nadu", "Telangana", "Tripura",

"Uttar Pradesh", "Uttarakhand", "West Bengal"

};

public StateSelectionApp() {

setTitle("State Selection App");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

stateComboBox = new JComboBox<>(states);

selectedStateLabel = new JLabel("Selected State:");

stateComboBox.addActionListener(this);

setLayout(new FlowLayout());

add(new JLabel("Select a State:"));

add(stateComboBox);

add(selectedStateLabel);

@Override

public void actionPerformed(ActionEvent e) {

String selectedState = (String) stateComboBox.getSelectedItem();

selectedStateLabel.setText("Selected State: " + selectedState);

23
Devansh Chhatre

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

StateSelectionApp app = new StateSelectionApp();

app.setVisible(true);

});

2) Develop a program to demonstrate the use of ScrollPane in Swings


import javax.swing.*;

import java.awt.*;

public class ScrollPaneDemo extends JFrame {

public ScrollPaneDemo() {

setTitle("Scroll Pane Demo");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextArea textArea = new JTextArea(10, 20);

JScrollPane scrollPane = new JScrollPane(textArea);

// Add some text to the text area

textArea.setText("This is a demo of JScrollPane in Java Swing.\n"

+ "ScrollPane allows you to scroll through\n"

24
Devansh Chhatre

+ "components that are larger than the visible area.");

setLayout(new BorderLayout());

add(scrollPane, BorderLayout.CENTER);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

ScrollPaneDemo app = new ScrollPaneDemo();

app.setVisible(true);

});

25
Devansh Chhatre

26

You might also like