[go: up one dir, main page]

0% found this document useful (0 votes)
8 views13 pages

Assignment - 13 09 24

The document contains Java programs demonstrating various GUI functionalities using Swing. It includes examples of creating a button that displays a menu, generating a beep sound, performing linked list operations, using GridBagLayout, and changing the background color based on user selection from a combo box. Each program is designed to showcase specific features of Java Swing and user interaction.

Uploaded by

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

Assignment - 13 09 24

The document contains Java programs demonstrating various GUI functionalities using Swing. It includes examples of creating a button that displays a menu, generating a beep sound, performing linked list operations, using GridBagLayout, and changing the background color based on user selection from a combo box. Each program is designed to showcase specific features of Java Swing and user interaction.

Uploaded by

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

Name: SHASHI VAMSHI V

Usn: 1BM23MC088
Subject: JAVA

1) write a program to create a button on container when press the button it display menu of items for
the user choose from in java

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

public class MenuButtonExample {

private JFrame frame;


private JButton button;
private JPopupMenu popupMenu;

public MenuButtonExample() {
// Initialize the frame
frame = new JFrame("Menu Button Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

// Initialize the button


button = new JButton("Show Menu");

// Initialize the popup menu


popupMenu = new JPopupMenu();
// Add menu items
JMenuItem item1 = new JMenuItem("Option 1");
JMenuItem item2 = new JMenuItem("Option 2");
JMenuItem item3 = new JMenuItem("Option 3");

// Add action listeners to menu items


item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Option 1 selected");
}
});

item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Option 2 selected");
}
});

item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Option 3 selected");
}
});

// Add items to popup menu


popupMenu.add(item1);
popupMenu.add(item2);
popupMenu.add(item3);
// Set button action to show popup menu
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Show popup menu below the button
popupMenu.show(button, 0, button.getHeight());
}
});

// Add button to frame


frame.add(button);

// Make the frame visible


frame.setVisible(true);
}

public static void main(String[] args) {


// Run the GUI code on the Event Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Initialize the GUI
new MenuButtonExample();
}
});
}
}
2) define a container which accepts the user input through the button when i click the button it
generates beep sound
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BeepButtonExample {

private JFrame frame;


private JButton beepButton;

public BeepButtonExample() {
// Initialize the frame
frame = new JFrame("Beep Button Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());

// Initialize the button


beepButton = new JButton("Generate Beep");

// Set button action to generate beep sound


beepButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Generate a beep sound
Toolkit.getDefaultToolkit().beep();
}
});
// Add button to frame
frame.add(beepButton);

// Make the frame visible


frame.setVisible(true);
}

public static void main(String[] args) {


// Run the GUI code on the Event Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Initialize the GUI
new BeepButtonExample();
}
});
}
}

3) develop a java program to swing to demonstrate linked list operations

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

public class LinkedListDemo {

private JFrame frame;


private JTextField inputField;
private JButton addButton;
private JButton removeButton;
private JList<String> listDisplay;
private DefaultListModel<String> listModel;
private LinkedList<String> linkedList;

public LinkedListDemo() {
// Initialize the linked list
linkedList = new LinkedList<>();

// Initialize the frame


frame = new JFrame("Linked List Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());

// Initialize the input field


inputField = new JTextField(20);

// Initialize buttons
addButton = new JButton("Add");
removeButton = new JButton("Remove");

// Initialize the list model and JList


listModel = new DefaultListModel<>();
listDisplay = new JList<>(listModel);

// Panel for input and buttons


JPanel panel = new JPanel();
panel.add(new JLabel("Item:"));
panel.add(inputField);
panel.add(addButton);
panel.add(removeButton);

// Add components to the frame


frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(listDisplay), BorderLayout.CENTER);

// Set button actions


addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String item = inputField.getText().trim();
if (!item.isEmpty()) {
linkedList.add(item);
updateListDisplay();
inputField.setText(""); // Clear the input field
} else {
JOptionPane.showMessageDialog(frame, "Please enter an item.");
}
}
});

removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String item = inputField.getText().trim();
if (linkedList.remove(item)) {
updateListDisplay();
inputField.setText(""); // Clear the input field
} else {
JOptionPane.showMessageDialog(frame, "Item not found.");
}
}
});

// Make the frame visible


frame.setVisible(true);
}

private void updateListDisplay() {


listModel.clear();
for (String item : linkedList) {
listModel.addElement(item);
}
}

public static void main(String[] args) {


// Run the GUI code on the Event Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LinkedListDemo();
}
});
}
}

4) demonstrate the functionality of Grid Bag Layout with suitable example


import javax.swing.*;
import java.awt.*;

public class GridBagLayoutExample {

public static void main(String[] args) {


// Use anonymous inner class instead of lambda expression
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagLayoutExample().createAndShowGUI();
}
});
}

private void createAndShowGUI() {


// Create the frame
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new GridBagLayout());

// Create constraints object


GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5); // Padding around components

// Create and add components


// Label 1
JLabel nameLabel = new JLabel("Name:");
gbc.gridx = 0; // Column
gbc.gridy = 0; // Row
gbc.weightx = 0.1; // Horizontal weight
frame.add(nameLabel, gbc);

// TextField 1
JTextField nameField = new JTextField();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.9; // Fill available horizontal space
gbc.gridwidth = GridBagConstraints.REMAINDER; // Span to the end of the row
frame.add(nameField, gbc);
gbc.gridwidth = 1; // Reset gridwidth

// Label 2
JLabel emailLabel = new JLabel("Email:");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.1;
frame.add(emailLabel, gbc);

// TextField 2
JTextField emailField = new JTextField();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 0.9;
frame.add(emailField, gbc);

// Button
JButton submitButton = new JButton("Submit");
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 0.0;
gbc.anchor = GridBagConstraints.EAST; // Align to the end of the cell
frame.add(submitButton, gbc);

// Display the frame


frame.setVisible(true);
}
}

5) create a fame with a back down box which contains different colors in it based on color choose in that
same background color should change
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ColorChangeExample {

private JFrame frame;


private JComboBox<String> colorComboBox;

public ColorChangeExample() {
// Create the frame
frame = new JFrame("Background Color Changer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
// Create an array of color names
String[] colors = { "Select Color", "Red", "Green", "Blue", "Yellow", "Cyan", "Magenta" };

// Initialize the combo box with color names


colorComboBox = new JComboBox<>(colors);

// Add ActionListener to the combo box


colorComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected color name
String selectedColor = (String) colorComboBox.getSelectedItem();

// Set the background color based on the selected color


switch (selectedColor) {
case "Red":
frame.getContentPane().setBackground(Color.RED);
break;
case "Green":
frame.getContentPane().setBackground(Color.GREEN);
break;
case "Blue":
frame.getContentPane().setBackground(Color.BLUE);
break;
case "Yellow":
frame.getContentPane().setBackground(Color.YELLOW);
break;
case "Cyan":
frame.getContentPane().setBackground(Color.CYAN);
break;
case "Magenta":
frame.getContentPane().setBackground(Color.MAGENTA);
break;
default:
frame.getContentPane().setBackground(null); // Reset to default color
break;
}
}
});

// Add the combo box to the frame


frame.add(colorComboBox, BorderLayout.NORTH);

// Set the frame to be visible


frame.setVisible(true);
}

public static void main(String[] args) {


// Use an anonymous inner class instead of lambda expressions
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ColorChangeExample();
}
});
}
}

You might also like