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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class TodoList extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JButton btnAdd;
private JPanel rightPane;
private JList list;
private DefaultListModel<String> model;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TodoList frame = new TodoList();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TodoList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JSplitPane splitPane = new JSplitPane();
contentPane.add(splitPane);
JPanel leftPane = new JPanel();
splitPane.setLeftComponent(leftPane);
textField = new JTextField();
leftPane.add(textField);
textField.setColumns(10);
rightPane = new JPanel();
splitPane.setRightComponent(rightPane);
model = new DefaultListModel<>();
rightPane.setLayout(new BorderLayout(0, 0));
list = new JList(model);
rightPane.add(list);
btnAdd = new JButton("Add");
leftPane.add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.getText().isEmpty()) {
model.addElement(textField.getText());
textField.setText("");
}
}
});
}
} |
Partager