Closed hoangbuituananh132109 closed 5 months ago
import javax.swing.; import java.awt.; import java.awt.event.; import java.io.;
public class SimpleNotepad extends JFrame implements ActionListener { private JTextArea textArea; private JFileChooser fileChooser; private String currentFile;
public SimpleNotepad() { setTitle("Simple Notepad"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem newMenuItem = new JMenuItem("New"); newMenuItem.addActionListener(this); JMenuItem openMenuItem = new JMenuItem("Open"); openMenuItem.addActionListener(this); JMenuItem saveMenuItem = new JMenuItem("Save"); saveMenuItem.addActionListener(this); fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); menuBar.add(fileMenu); setJMenuBar(menuBar); fileChooser = new JFileChooser(); setVisible(true); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("New")) { textArea.setText(""); currentFile = null; } else if (command.equals("Open")) { int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { FileReader reader = new FileReader(file); textArea.read(reader, null); reader.close(); currentFile = file.getAbsolutePath(); } catch (IOException ex) { ex.printStackTrace(); } } } else if (command.equals("Save")) { if (currentFile != null) { try { FileWriter writer = new FileWriter(currentFile); textArea.write(writer); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } else { int returnVal = fileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { FileWriter writer = new FileWriter(file); textArea.write(writer); writer.close(); currentFile = file.getAbsolutePath(); } catch (IOException ex) { ex.printStackTrace(); } } } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SimpleNotepad(); } }); }
}
import javax.swing.; import java.awt.; import java.awt.event.; import java.io.;
public class SimpleNotepad extends JFrame implements ActionListener { private JTextArea textArea; private JFileChooser fileChooser; private String currentFile;
}