帮我补充这段代码便签管理部分的代码框架: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Memo extends JFrame { private JTextField titleField; private JTextArea contentArea; private JTextField timeField; private JButton addButton; private JButton editButton; private JButton deleteButton; private JButton viewButton; private JList memoList; private DefaultListModel listModel; public Memo() { // 初始化界面和组件 // 添加事件监听器 // 实现添加、编辑、删除和查看便签信息的功能 } public static void main(String[] args) { Memo memo = new Memo(); memo.setVisible(true); } }
时间: 2024-03-15 11:41:40 浏览: 75
JAVA 程序 源代码 import java.applet.*;import java.awt.*
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Memo extends JFrame {
private JTextField titleField;
private JTextArea contentArea;
private JTextField timeField;
private JButton addButton;
private JButton editButton;
private JButton deleteButton;
private JButton viewButton;
private JList memoList;
private DefaultListModel listModel;
public Memo() {
// 初始化界面和组件
setTitle("便签管理");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocationRelativeTo(null);
JPanel topPanel = new JPanel(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(1, 3));
titleField = new JTextField();
contentArea = new JTextArea();
timeField = new JTextField();
inputPanel.add(titleField);
inputPanel.add(contentArea);
inputPanel.add(timeField);
topPanel.add(inputPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
addButton = new JButton("添加");
editButton = new JButton("编辑");
deleteButton = new JButton("删除");
viewButton = new JButton("查看");
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(viewButton);
topPanel.add(buttonPanel, BorderLayout.SOUTH);
add(topPanel, BorderLayout.NORTH);
listModel = new DefaultListModel();
memoList = new JList(listModel);
JScrollPane scrollPane = new JScrollPane(memoList);
add(scrollPane, BorderLayout.CENTER);
// 添加事件监听器
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 添加便签信息
String title = titleField.getText();
String content = contentArea.getText();
String time = timeField.getText();
MemoData memo = new MemoData(title, content, time);
listModel.addElement(memo);
}
});
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 编辑便签信息
int index = memoList.getSelectedIndex();
if (index != -1) {
String title = titleField.getText();
String content = contentArea.getText();
String time = timeField.getText();
MemoData memo = new MemoData(title, content, time);
listModel.remove(index);
listModel.add(index, memo);
}
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 删除便签信息
int index = memoList.getSelectedIndex();
if (index != -1) {
listModel.remove(index);
}
}
});
viewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 查看便签信息
int index = memoList.getSelectedIndex();
if (index != -1) {
MemoData memo = (MemoData) listModel.getElementAt(index);
titleField.setText(memo.getTitle());
contentArea.setText(memo.getContent());
timeField.setText(memo.getTime());
}
}
});
}
public static void main(String[] args) {
Memo memo = new Memo();
memo.setVisible(true);
}
}
class MemoData {
private String title;
private String content;
private String time;
public MemoData(String title, String content, String time) {
this.title = title;
this.content = content;
this.time = time;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getTime() {
return time;
}
@Override
public String toString() {
return title + " - " + time;
}
}
阅读全文