帮我用java写一个桌面便签吧
时间: 2024-01-01 17:04:47 浏览: 39
好的,下面是一个简单的Java桌面便签的示例代码,你可以参考一下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DesktopMemo extends JFrame implements ActionListener {
private JTextArea memo;
private JButton saveBtn, loadBtn;
public DesktopMemo() {
super("桌面便签");
memo = new JTextArea(5, 30);
saveBtn = new JButton("保存");
loadBtn = new JButton("读取");
JPanel btnPanel = new JPanel();
btnPanel.add(saveBtn);
btnPanel.add(loadBtn);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JScrollPane(memo), BorderLayout.CENTER);
contentPane.add(btnPanel, BorderLayout.SOUTH);
saveBtn.addActionListener(this);
loadBtn.addActionListener(this);
setResizable(false);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveBtn) {
String memoStr = memo.getText();
// 将memoStr保存到文件中
// ...
JOptionPane.showMessageDialog(this, "保存成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == loadBtn) {
// 从文件中读取memoStr
// ...
memo.setText(memoStr);
JOptionPane.showMessageDialog(this, "读取成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
new DesktopMemo();
}
}
```
运行程序后,你可以在窗口中输入文本,然后点击“保存”按钮将文本保存到文件中,点击“读取”按钮可以从文件中读取文本。需要注意的是,这只是一个简单的示例代码,实际使用时还需要进行文件操作、数据校验等相关的处理。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)