OneCompiler

gui

53

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

public class SimpleCalculator implements ActionListener {

JFrame frame;
JTextField textField;
String operator = "";
double num1 = 0, num2 = 0, result = 0;

SimpleCalculator() {
    frame = new JFrame("Calculator");
    textField = new JTextField();
    textField.setBounds(30, 20, 280, 40);
    textField.setEditable(false);

    frame.add(textField);

    // Buttons
    String[] buttons = {
        "7", "8", "9", "/",
        "4", "5", "6", "*",
        "1", "2", "3", "-",
        "0", "C", "=", "+"
    };

    JPanel panel = new JPanel();
    panel.setBounds(30, 80, 280, 300);
    panel.setLayout(new GridLayout(4, 4, 10, 10));

    for (String text : buttons) {
        JButton btn = new JButton(text);
        btn.addActionListener(this);
        panel.add(btn);
    }

    frame.add(panel);

    frame.setSize(350, 450);
    frame.setLayout(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();

    if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {
        textField.setText(textField.getText() + command);
    } 
    else if (command.equals("C")) {
        textField.setText("");
        num1 = num2 = result = 0;
    } 
    else if (command.equals("=")) {
        num2 = Double.parseDouble(textField.getText());

        switch (operator) {
            case "+": result = num1 + num2; break;
            case "-": result = num1 - num2; break;
            case "*": result = num1 * num2; break;
            case "/": result = num1 / num2; break;
        }

        textField.setText("" + result);
    } 
    else {
        num1 = Double.parseDouble(textField.getText());
        operator = command;
        textField.setText("");
    }
}

public static void main(String[] args) {
    new SimpleCalculator();
}

}


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

public class RegistrationForm {

public static void main(String[] args) {

    JFrame frame = new JFrame("Registration Form");
    JPanel panel = new JPanel();
    panel.setLayout(null);

    // Name
    JLabel nameLabel = new JLabel("Name:");
    nameLabel.setBounds(30, 30, 100, 25);
    panel.add(nameLabel);

    JTextField nameField = new JTextField();
    nameField.setBounds(150, 30, 150, 25);
    panel.add(nameField);

    // Email
    JLabel emailLabel = new JLabel("Email:");
    emailLabel.setBounds(30, 70, 100, 25);
    panel.add(emailLabel);

    JTextField emailField = new JTextField();
    emailField.setBounds(150, 70, 150, 25);
    panel.add(emailField);

    // Gender
    JLabel genderLabel = new JLabel("Gender:");
    genderLabel.setBounds(30, 110, 100, 25);
    panel.add(genderLabel);

    JRadioButton male = new JRadioButton("Male");
    male.setBounds(150, 110, 70, 25);

    JRadioButton female = new JRadioButton("Female");
    female.setBounds(220, 110, 80, 25);

    ButtonGroup bg = new ButtonGroup();
    bg.add(male);
    bg.add(female);

    panel.add(male);
    panel.add(female);

    // Course
    JLabel courseLabel = new JLabel("Course:");
    courseLabel.setBounds(30, 150, 100, 25);
    panel.add(courseLabel);

    String[] courses = {"B.Tech", "BCA", "MCA", "MBA"};
    JComboBox<String> courseBox = new JComboBox<>(courses);
    courseBox.setBounds(150, 150, 150, 25);
    panel.add(courseBox);

    // Submit Button
    JButton submitBtn = new JButton("Submit");
    submitBtn.setBounds(120, 200, 100, 30);
    panel.add(submitBtn);

    // Action
    submitBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name = nameField.getText();
            String email = emailField.getText();
            String gender = male.isSelected() ? "Male" : "Female";
            String course = (String) courseBox.getSelectedItem();

            JOptionPane.showMessageDialog(frame,
                    "Name: " + name +
                    "\nEmail: " + email +
                    "\nGender: " + gender +
                    "\nCourse: " + course);
        }
    });

    // Frame setup
    frame.add(panel);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}


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

public class LayoutDemo {

public static void main(String[] args) {

    JFrame frame = new JFrame("Layout Managers Demo");
    frame.setSize(500, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Main container with BorderLayout
    frame.setLayout(new BorderLayout());

    // 🔴 FLOW LAYOUT (Top)
    JPanel flowPanel = new JPanel();
    flowPanel.setLayout(new FlowLayout());
    flowPanel.add(new JButton("Button 1"));
    flowPanel.add(new JButton("Button 2"));
    flowPanel.add(new JButton("Button 3"));

    // 🔵 GRID LAYOUT (Center)
    JPanel gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(2, 2, 10, 10));
    gridPanel.add(new JButton("G1"));
    gridPanel.add(new JButton("G2"));
    gridPanel.add(new JButton("G3"));
    gridPanel.add(new JButton("G4"));

    // 🟢 BORDER LAYOUT (Bottom example inside panel)
    JPanel borderPanel = new JPanel();
    borderPanel.setLayout(new BorderLayout());
    borderPanel.add(new JButton("West"), BorderLayout.WEST);
    borderPanel.add(new JButton("East"), BorderLayout.EAST);
    borderPanel.add(new JButton("Center"), BorderLayout.CENTER);

    // Add panels to frame
    frame.add(flowPanel, BorderLayout.NORTH);
    frame.add(gridPanel, BorderLayout.CENTER);
    frame.add(borderPanel, BorderLayout.SOUTH);

    frame.setVisible(true);
}

}


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

public class LoginForm {

public static void main(String[] args) {

    JFrame frame = new JFrame("Login Form");
    frame.setSize(350, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);

    // Username
    JLabel userLabel = new JLabel("Username:");
    userLabel.setBounds(30, 30, 100, 25);
    frame.add(userLabel);

    JTextField userField = new JTextField();
    userField.setBounds(130, 30, 150, 25);
    frame.add(userField);

    // Password
    JLabel passLabel = new JLabel("Password:");
    passLabel.setBounds(30, 70, 100, 25);
    frame.add(passLabel);

    JPasswordField passField = new JPasswordField();
    passField.setBounds(130, 70, 150, 25);
    frame.add(passField);

    // Login Button
    JButton loginBtn = new JButton("Login");
    loginBtn.setBounds(110, 110, 100, 30);
    frame.add(loginBtn);

    // Hard-coded credentials
    String correctUser = "admin";
    String correctPass = "1234";

    // Event Handling
    loginBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String username = userField.getText();
            String password = new String(passField.getPassword());

            if (username.equals(correctUser) && password.equals(correctPass)) {
                JOptionPane.showMessageDialog(frame, "Login Successful ✅");
            } else {
                JOptionPane.showMessageDialog(frame, "Invalid Username or Password ❌");
            }
        }
    });

    frame.setVisible(true);
}

}


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

public class EventDemo implements MouseListener, KeyListener {

JFrame frame;
JLabel label;

EventDemo() {
    frame = new JFrame("Mouse & Keyboard Event Demo");
    frame.setSize(400, 300);
    frame.setLayout(new FlowLayout());

    label = new JLabel("Interact using mouse or keyboard");
    label.setFont(new Font("Arial", Font.BOLD, 14));
    frame.add(label);

    // Add listeners
    frame.addMouseListener(this);
    frame.addKeyListener(this);

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

// 🔴 Mouse Events
public void mouseClicked(MouseEvent e) {
    label.setText("Mouse Clicked");
}

public void mousePressed(MouseEvent e) {
    label.setText("Mouse Pressed");
}

public void mouseReleased(MouseEvent e) {
    label.setText("Mouse Released");
}

public void mouseEntered(MouseEvent e) {
    label.setText("Mouse Entered Window");
}

public void mouseExited(MouseEvent e) {
    label.setText("Mouse Exited Window");
}

// 🔵 Keyboard Events
public void keyTyped(KeyEvent e) {
    label.setText("Key Typed: " + e.getKeyChar());
}

public void keyPressed(KeyEvent e) {
    label.setText("Key Pressed: " + e.getKeyChar());
}

public void keyReleased(KeyEvent e) {
    label.setText("Key Released: " + e.getKeyChar());
}

public static void main(String[] args) {
    new EventDemo();
}

}


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

public class AdapterDemo {

public static void main(String[] args) {

    JFrame frame = new JFrame("MouseAdapter Demo");
    frame.setSize(400, 300);
    frame.setLayout(new FlowLayout());

    JLabel label = new JLabel("Click anywhere in the window");
    label.setFont(new Font("Arial", Font.BOLD, 14));
    frame.add(label);

    // Using MouseAdapter (only mouseClicked)
    frame.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            label.setText("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");
        }
    });

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

}


import javax.swing.;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.
;

public class JTreeDemo {

public static void main(String[] args) {

    JFrame frame = new JFrame("JTree with JScrollPane");
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Root node
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Courses");

    // Child nodes
    DefaultMutableTreeNode cs = new DefaultMutableTreeNode("Computer Science");
    DefaultMutableTreeNode mech = new DefaultMutableTreeNode("Mechanical");

    // Sub-child nodes
    cs.add(new DefaultMutableTreeNode("Data Structures"));
    cs.add(new DefaultMutableTreeNode("Operating Systems"));

    mech.add(new DefaultMutableTreeNode("Thermodynamics"));
    mech.add(new DefaultMutableTreeNode("Fluid Mechanics"));

    // Add children to root
    root.add(cs);
    root.add(mech);

    // Create JTree
    JTree tree = new JTree(root);

    // Add JTree to JScrollPane
    JScrollPane scrollPane = new JScrollPane(tree);

    // Add scrollPane to frame
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setVisible(true);
}

}


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

public class ColorChooserDemo implements ActionListener {

JFrame frame;
JButton button;

ColorChooserDemo() {
    frame = new JFrame("Color Chooser Demo");
    frame.setSize(400, 300);
    frame.setLayout(new FlowLayout());

    button = new JButton("Choose Color");
    button.addActionListener(this);

    frame.add(button);

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

public void actionPerformed(ActionEvent e) {

    // Open color chooser dialog
    Color color = JColorChooser.showDialog(frame, "Select a Color", Color.WHITE);

    // If user selects a color
    if (color != null) {
        frame.getContentPane().setBackground(color);
    }
}

public static void main(String[] args) {
    new ColorChooserDemo();
}

}


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

public class JTableDemo {

public static void main(String[] args) {

    JFrame frame = new JFrame("JTable Example");
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // 2D data (rows × columns)
    String[][] data = {
        {"1", "Sudarshan", "CSE"},
        {"2", "Rahul", "IT"},
        {"3", "Anjali", "ECE"},
        {"4", "Priya", "ME"}
    };

    // Column names
    String[] columnNames = {"ID", "Name", "Branch"};

    // Create JTable
    JTable table = new JTable(data, columnNames);

    // Add table to scroll pane
    JScrollPane scrollPane = new JScrollPane(table);

    // Add to frame
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setVisible(true);
}

}