We read your photo as:

Create a basic Java Swing UI for an attendance system.

Computer Science

ScanSolve AI Answer

Here's the Java code for a simple user interface layout for the attendance system:

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

public class AttendanceSystemUI extends JFrame {

    private JTextField studentNameField;
    private JTextField studentIdField;
    private JRadioButton presentRadioButton;
    private JRadioButton absentRadioButton;
    private JRadioButton lateRadioButton;
    private ButtonGroup attendanceGroup;
    private JButton submitButton;

    public AttendanceSystemUI() {
        // Step 1: Set up the main frame
        setTitle("EduTech Academy Attendance System");
        setSize(400, 250); // Increased height for better spacing
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Center the window

        // Step 2: Create panels for organization
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); // Main panel with some padding
        JPanel inputPanel = new JPanel(new GridLayout(3, 2, 5, 5)); // Grid for labels and text fields
        JPanel attendancePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); // Flow for radio buttons
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // Flow for submit button

        // Step 3: Initialize UI components
        // Student Name
        inputPanel.add(new JLabel("Student Name:"));
        studentNameField = new JTextField(20);
        inputPanel.add(studentNameField);

        // Student ID
        inputPanel.add(new JLabel("Student ID:"));
        studentIdField = new JTextField(20);
        inputPanel.add(studentIdField);

        // Attendance Selection
        attendancePanel.add(new JLabel("Attendance Status:"));
        presentRadioButton = new JRadioButton("Present");
        absentRadioButton = new JRadioButton("Absent");
        lateRadioButton = new JRadioButton("Late");

        attendanceGroup = new ButtonGroup();
        attendanceGroup.add(presentRadioButton);
        attendanceGroup.add(absentRadioButton);
        attendanceGroup.add(lateRadioButton);

        // Select 'Present' by default
        presentRadioButton.setSelected(true);

        attendancePanel.add(presentRadioButton);
        attendancePanel.add(absentRadioButton);
        attendancePanel.add(lateRadioButton);

        // Submit Button
        submitButton = new JButton("Submit");
        buttonPanel.add(submitButton);

        // Step 4: Add components to the main panel
        mainPanel.add(inputPanel, BorderLayout.NORTH);
        mainPanel.add(attendancePanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // Add padding to the main panel
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // Add the main panel to the frame
        add(mainPanel);

        // Step 5: Add action listener for the submit button (for demonstration)
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentName = studentNameField.getText();
                String studentId = studentIdField.getText();
                String attendanceStatus = "";

                if (presentRadioButton.isSelected()) {
                    attendanceStatus = "Present";
                } else if (absentRadioButton.isSelected()) {
                    attendanceStatus = "Absent";
                } else if (lateRadioButton.isSelected()) {
                    attendanceStatus = "Late";
                }

                // For a real system, this data would be saved to a database or file.
                // Here, we just display it in a dialog.
                JOptionPane.showMessageDialog(AttendanceSystemUI.this,
                        "Student Name: " + studentName + "\n" +
                        "Student ID: " + studentId + "\n" +
                        "Attendance Status: " + attendanceStatus,
                        "Attendance Recorded",
                        JOptionPane.INFORMATION_MESSAGE);

                // Clear fields after submission
                studentNameField.setText("");
                studentIdField.setText("");
                presentRadioButton.setSelected(true); // Reset to default
            }
        });
    }

    public static void main(String[] args) {
        // Ensure the UI is created and updated on the Event Dispatch Thread
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AttendanceSystemUI().setVisible(true);
            }
        });
    }
}

Explanation:

  1. JFrame: This is the main window of the application. We set its title, size, default close operation, and center it on the screen.
  2. JPanel: Used for organizing components.
    • mainPanel: Uses BorderLayout to arrange the input fields at the top (NORTH), attendance selection in the middle (CENTER), and the submit button at the bottom (SOUTH).
    • inputPanel: Uses GridLayout to arrange the "Student Name" and "Student ID" labels with their respective text fields in a 3x2 grid.
    • attendancePanel: Uses FlowLayout to arrange the "Attendance Status" label and the radio buttons horizontally.
    • buttonPanel: Uses FlowLayout to center the "Submit" button.
  3. JLabel: Used to display static text labels like "Student Name:", "Student ID:", and "Attendance Status:".
  4. JTextField: These are editable text boxes where the teacher can enter the student's name and ID.
  5. JRadioButton and ButtonGroup: JRadioButtons allow the selection of one option from a set. The ButtonGroup ensures that only one of "Present," "Absent," or "Late" can be selected at a time. "Present" is set as the default selection.
  6. JButton: This is the "Submit" button. An ActionListener is added to it to demonstrate what happens when it's clicked (in a real application, this would save data to a database; here, it shows a confirmation dialog).
  7. SwingUtilities.invokeLater: This ensures that the GUI creation and updates are performed on the Event Dispatch Thread (EDT), which is crucial for thread-safe Swing applications.

This code provides a functional and clear user interface layout as requested.

Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question