Ajp Prac QB Ans

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Q1: Textbox, Radio Button, and Checkbox

java

Copy code

import javax.swing.*;

import java.awt.*;

public class GUIExample extends JFrame {

public GUIExample() {

JTextField textBox = new JTextField("Enter text here");

JRadioButton radioButton = new JRadioButton("Radio button");

JCheckBox checkBox = new JCheckBox("Checkbox");

setLayout(new FlowLayout());

add(textBox);

add(radioButton);

add(checkBox);

setSize(300, 150);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new GUIExample());

Q2: Choice & List for Various Cities in India

java

Copy code

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

public class CityListExample extends JFrame {

public CityListExample() {

String[] cities = {"Mumbai", "Delhi", "Bangalore", "Chennai"};

JComboBox<String> choice = new JComboBox<>(cities);

JList<String> cityList = new JList<>(cities);

setLayout(new FlowLayout());

add(choice);

add(cityList);

setSize(300, 150);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new CityListExample());

For Q3 to Q12, I'll provide the code in separate responses to keep it organized. Let me know if you'd
like anything specific for each example.

User

Okay now answer all that questions


ChatGPT

Certainly! Here are examples for each of the remaining questions:

Q3: Border Layout, Card Layout & Grid Layout

java

Copy code

import javax.swing.*;

import java.awt.*;

public class LayoutExample extends JFrame {

public LayoutExample() {

setLayout(new BorderLayout());

JPanel borderPanel = new JPanel();

borderPanel.add(new JLabel("Border Layout"));

add(borderPanel, BorderLayout.NORTH);

JPanel cardPanel = new JPanel(new CardLayout());

cardPanel.add(new JLabel("Card Layout 1"), "card1");

cardPanel.add(new JLabel("Card Layout 2"), "card2");

add(cardPanel, BorderLayout.CENTER);

JPanel gridPanel = new JPanel(new GridLayout(2, 2));

gridPanel.add(new JLabel("Grid 1"));

gridPanel.add(new JLabel("Grid 2"));

gridPanel.add(new JLabel("Grid 3"));

gridPanel.add(new JLabel("Grid 4"));

add(gridPanel, BorderLayout.SOUTH);

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new LayoutExample());

Q4: Menu & Menu Items

java

Copy code

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class MenuExample extends JFrame {

public MenuExample() {

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("File");

JMenuItem menuItem = new JMenuItem("Exit");

menuItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

System.exit(0);

});

menu.add(menuItem);

menuBar.add(menu);

setJMenuBar(menuBar);
setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new MenuExample());

Q5: Card Layout

java

Copy code

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class CardLayoutExample extends JFrame {

public CardLayoutExample() {

CardLayout cardLayout = new CardLayout();

setLayout(cardLayout);

JButton card1Button = new JButton("Card 1");

JButton card2Button = new JButton("Card 2");

add(card1Button, "card1");

add(card2Button, "card2");

card1Button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

cardLayout.show(getContentPane(), "card1");

});

card2Button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

cardLayout.show(getContentPane(), "card2");

});

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new CardLayoutExample());

Q6: Grid Layout

java

Copy code

import javax.swing.*;

import java.awt.*;

public class GridLayoutExample extends JFrame {

public GridLayoutExample() {

setLayout(new GridLayout(2, 2));


add(new JButton("Button 1"));

add(new JButton("Button 2"));

add(new JButton("Button 3"));

add(new JButton("Button 4"));

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new GridLayoutExample());

Q7: Combo Box for Various Cities in India and Display the Selected City

java

Copy code

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class ComboBoxExample extends JFrame {

public ComboBoxExample() {

String[] cities = {"Mumbai", "Delhi", "Bangalore", "Chennai"};

JComboBox<String> cityComboBox = new JComboBox<>(cities);

JLabel selectedCityLabel = new JLabel("Selected City: ");

cityComboBox.addActionListener(new ActionListener() {
@Override

public void actionPerformed(ActionEvent e) {

String selectedCity = (String) cityComboBox.getSelectedItem();

selectedCityLabel.setText("Selected City: " + selectedCity);

});

setLayout(new FlowLayout());

add(cityComboBox);

add(selectedCityLabel);

setSize(300, 150);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new ComboBoxExample());

Q8: JTree & Its Nodes

java

Copy code

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

public class JTreeExample extends JFrame {

public JTreeExample() {

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");


DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Node 1");

DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Node 2");

rootNode.add(node1);

rootNode.add(node2);

JTree tree = new JTree(rootNode);

add(new JScrollPane(tree));

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new JTreeExample());

Q9: JTable for Various Employees in a Company

java

Copy code

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

public class JTableExample extends JFrame {

public JTableExample() {

String[] columnNames = {"Name", "Position", "Salary"};

Object[][] data = {

{"John Doe", "Developer", 60000},


{"Jane Smith", "Manager", 80000},

{"Bob Johnson", "Analyst", 55000}

};

DefaultTableModel model = new DefaultTableModel(data, columnNames);

JTable table = new JTable(model);

add(new JScrollPane(table));

setSize(400, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new JTableExample());

Q10: Password Field and Display Username & Password on Screen

java

Copy code

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class PasswordFieldExample extends JFrame {

public PasswordFieldExample() {

JTextField usernameField = new JTextField(15);

JPasswordField passwordField = new JPasswordField(15);

JButton loginButton = new JButton("Login");


loginButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String username = usernameField.getText();

char[] passwordChars = passwordField.getPassword();

String password = new String(passwordChars);

JOptionPane.showMessageDialog(null, "Username: " + username + "\nPassword: " +


password);

});

setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

add(new JLabel("Username:"));

add(usernameField);

add(new JLabel("Password:"));

add(passwordField);

add(loginButton);

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new PasswordFieldExample());

Q11: All InetAddress Methods in Java

java
Copy code

import java.net.InetAddress;

import java.net.UnknownHostException;

public class InetAddressExample {

public static void main(String[] args) {

try {

InetAddress localHost = InetAddress.getLocalHost();

System.out.println("Local Host: " + localHost);

InetAddress[] allByName = InetAddress.getAllByName("www.google.com");

System.out.println("All IP Addresses for www.google.com:");

for (InetAddress address : allByName) {

System.out.println(address);

} catch (UnknownHostException e) {

e.printStackTrace();

Q12: All URL Methods in Java

java

Copy code

import java.net.MalformedURLException;

import java.net.URL;

public class URLExample {

public static void main(String[] args) {

try {

String urlString = "https://www.example.com/path?query=123";


URL url = new URL(urlString);

System.out.println("Protocol: " + url.getProtocol());

System.out.println("Host: " + url.getHost());

System.out.println("Port: " + url.getPort());

System.out.println("Path: " + url.getPath());

System.out.println("Query: " + url.getQuery());

System.out.println("File: " + url.getFile());

} catch (MalformedURLException e) {

e.printStackTrace();

You might also like