import javax.swing.
*;
import java.awt.*;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import java.util.ArrayList;
public class StudentListApp
extends JFrame {
private JTextField nameField;
private JButton saveButton;
private JButton printAllButton;
private JList<String>
studentList;
private
DefaultListModel<String>
listModel;
private ArrayList<String>
students;
public StudentListApp() {
// Initialize components
nameField = new
JTextField(20);
saveButton = new
JButton("Save");
printAllButton = new
JButton("Print
All");
listModel = new
DefaultListModel<>();
studentList = new
JList<>(listModel);
students = new
ArrayList<>();
// Set up the layout
setLayout(new
BorderLayout());
JPanel inputPanel = new
JPanel();
inputPanel.add(new
JLabel("Full Names"));
inputPanel.add(nameField);
inputPanel.add(saveButton);
inputPanel.add(printAllButton);
add(inputPanel,
BorderLayout.NORTH);
add(new
JScrollPane(studentList),
BorderLayout.CENTER);
// Add action listeners
saveButton.addActionListener(ne
w ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
String name =
nameField.getText();
if (!name.isEmpty()) {
students.add(name);
listModel.addElement(name);
nameField.setText("");
}
}
});
printAllButton.addActionListener(
new ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
for (String student :
students) {
System.out.println(student);
}
}
});
// Set up the frame
setTitle("Student List App");
setDefaultCloseOperation(JFram
e.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
}
public static void main(String[]
args) {
SwingUtilities.invokeLater(new
Runnable() {
@Override
public void run() {
new
StudentListApp().setVisible(true);
}
});
}
}