自己做的一个简单的记事本

博客介绍了作者自制的一个简单记事本,但内容信息较少。

gjj

好的,以下是一个简易的记事本GUI的Java代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class SimpleNotepad extends JFrame implements ActionListener { private JTextArea textArea = new JTextArea(); private JMenuBar menuBar = new JMenuBar(); private JMenu fileMenu = new JMenu("File"); private JMenuItem newMenuItem = new JMenuItem("New"); private JMenuItem openMenuItem = new JMenuItem("Open"); private JMenuItem saveMenuItem = new JMenuItem("Save"); private JMenuItem exitMenuItem = new JMenuItem("Exit"); public SimpleNotepad() { setTitle("Simple Notepad"); // Set the size of the window setSize(640, 480); // Set the location of the window setLocationRelativeTo(null); // Add the text area to the content pane getContentPane().add(textArea); // Add the menu bar to the window setJMenuBar(menuBar); // Add the file menu to the menu bar menuBar.add(fileMenu); // Add the menu items to the file menu fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); // Add the action listeners to the menu items newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); // Show the window setVisible(true); } public void actionPerformed(ActionEvent e) { // If the user clicks the "New" menu item if (e.getSource() == newMenuItem) { // Clear the text area textArea.setText(""); } // If the user clicks the "Open" menu item else if (e.getSource() == openMenuItem) { // Create a file chooser dialog JFileChooser fileChooser = new JFileChooser(); // Show the dialog int result = fileChooser.showOpenDialog(this); // If the user clicks the "Open" button if (result == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); try { // Open the file and read its contents into the text area BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); } reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } // If the user clicks the "Save" menu item else if (e.getSource() == saveMenuItem) { // Create a file chooser dialog JFileChooser fileChooser = new JFileChooser(); // Show the dialog int result = fileChooser.showSaveDialog(this); // If the user clicks the "Save" button if (result == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); try { // Save the contents of the text area to the file BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } } // If the user clicks the "Exit" menu item else if (e.getSource() == exitMenuItem) { // Exit the application System.exit(0); } } public static void main(String[] args) { new SimpleNotepad(); } } ``` 这个记事本GUI包括了菜单栏和一个文本编辑区域,并且可以实现新建、打开、保存和退出功能。你可以根据自己的需求进行修改和扩展。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值