📄 tasklist.java
字号:
import java.awt.*;
import java.awt.event.*;
public class TaskList
extends Frame
implements ActionListener {
private Button add = new Button("Add");
private Button del = new Button("Delete");
private Button up = new Button(" + ");
private Button down = new Button("-");
private List list = new List();
private TextField taskInput = new TextField();
private Label priorityLabel = new Label("改变优先级");
private Label taskLabel = new Label("任务:");
private class WindowCloser
extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public TaskList() {
//构造方法定义布局和激活按钮
super("Task List");
setup();
add.addActionListener(this);
del.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
addWindowListener(new WindowCloser());
pack();
show();
}
public void actionPerformed(ActionEvent ae) {
//按下按钮后的反应
if ( (ae.getSource() == add) && (!taskInput.getText().equals(""))) {
handleAdd(taskInput.getText().trim());
}
else if ( (ae.getSource() == del) && (list.getSelectedIndex() >= 0)) {
handleDel(list.getSelectedIndex());
}
else if ( (ae.getSource() == up) && (list.getSelectedIndex() > 0)) {
handleIncPriority(list.getSelectedIndex());
}
else if ( (ae.getSource() == down) && (list.getSelectedIndex() >= 0)) {
handleDecPriority(list.getSelectedIndex());
}
taskInput.requestFocus();
}
private void handleAdd(String newTask) {
list.add(newTask);
list.select(list.getItemCount() - 1);
taskInput.setText("");
}
private void handleDel(int pos) {
list.remove(pos);
list.select(pos);
}
private void handleIncPriority(int pos) {
String item = list.getItem(pos);
list.remove(pos);
list.add(item, pos - 1);
list.select(pos - 1);
}
private void handleDecPriority(int pos) {
if (pos < list.getItemCount() - 1) {
String item = list.getItem(pos);
list.remove(pos);
list.add(item, pos + 1);
list.select(pos + 1);
}
}
private void setup() {
Panel buttons = new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(add);
buttons.add(del);
Panel priorities = new Panel();
priorities.setLayout(new FlowLayout());
priorities.add(up);
priorities.add(priorityLabel);
priorities.add(down);
Panel input = new Panel();
input.setLayout(new BorderLayout());
input.add("West", taskLabel);
input.add("Center", taskInput);
Panel top = new Panel();
top.setLayout(new GridLayout(2, 1));
top.add(input);
top.add(priorities);
setLayout(new BorderLayout());
add("Center", list);
add("South", buttons);
add("North", top);
}
public static void main(String args[]) {
TaskList tl = new TaskList();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -