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:
JFrame: This is the main window of the application. We set its title, size, default close operation, and center it on the screen.JPanel: Used for organizing components.mainPanel: UsesBorderLayoutto arrange the input fields at the top (NORTH), attendance selection in the middle (CENTER), and the submit button at the bottom (SOUTH).inputPanel: UsesGridLayoutto arrange the "Student Name" and "Student ID" labels with their respective text fields in a 3x2 grid.attendancePanel: UsesFlowLayoutto arrange the "Attendance Status" label and the radio buttons horizontally.buttonPanel: UsesFlowLayoutto center the "Submit" button.
JLabel: Used to display static text labels like "Student Name:", "Student ID:", and "Attendance Status:".JTextField: These are editable text boxes where the teacher can enter the student's name and ID.JRadioButtonandButtonGroup:JRadioButtons allow the selection of one option from a set. TheButtonGroupensures that only one of "Present," "Absent," or "Late" can be selected at a time. "Present" is set as the default selection.JButton: This is the "Submit" button. AnActionListeneris 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).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
More Computer Science Questions
QDevelop a basic Java UI layout for an attendance recording system for EduTech Academy.QWrite a Java program to display a product management system header and at least three product…QCreate Java code for a Laptop class that implements a StockInfo interface and tracks the total…QAnalyze the attendance system at Greenfield High School, detailing its challenges and reasons for…QAnalyze and report on the inefficiencies of Greenfield High School's manual attendance system…
Browse by topic:Java