-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoList.java
More file actions
38 lines (32 loc) · 1.23 KB
/
Copy pathTodoList.java
File metadata and controls
38 lines (32 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import javax.swing.*;
class TodoList {
public static void main(String[] args) {
JFrame frame = new JFrame("To-Do List");
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<>(model);
JTextField taskField = new JTextField();
JButton addButton = new JButton("Add");
JButton removeButton = new JButton("Remove");
taskField.setBounds(20, 20, 200, 30);
addButton.setBounds(230, 20, 80, 30);
removeButton.setBounds(320, 20, 90, 30);
list.setBounds(20, 60, 390, 200);
frame.add(taskField);
frame.add(addButton);
frame.add(removeButton);
frame.add(list);
addButton.addActionListener(e -> {
String task = taskField.getText();
if (!task.isEmpty()) model.addElement(task);
taskField.setText("");
});
removeButton.addActionListener(e -> {
int index = list.getSelectedIndex();
if (index != -1) model.remove(index);
});
frame.setSize(450, 320);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}