From ebd4714e021522c2a43c37e30eece563615634cc Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Wed, 31 Oct 2018 21:00:30 +0300 Subject: [PATCH 1/5] Add TextArea in TextEditor class --- src/editor/TextEditor.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/editor/TextEditor.java b/src/editor/TextEditor.java index 228c84b..1575f52 100644 --- a/src/editor/TextEditor.java +++ b/src/editor/TextEditor.java @@ -5,7 +5,32 @@ public class TextEditor extends JFrame { public TextEditor() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setSize(300, 300); setVisible(true); + setLayout(null); + setTitle("Text Editor"); + createTextArea(); + setSize(new Dimension(350, 300)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + } + + private void createTextArea() { + JTextArea textArea = new JTextArea(); + JScrollPane scrollPanepane = new JScrollPane(textArea); + textArea.setLineWrap(true); + textArea.setWrapStyleWord(true); + Container pane = getContentPane(); + GroupLayout groupLayout = new GroupLayout(pane); + pane.setLayout(groupLayout); + groupLayout.setAutoCreateContainerGaps(true); + groupLayout.getAutoCreateGaps(); + + groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup() + .addComponent(scrollPanepane)); + groupLayout.setVerticalGroup(groupLayout.createSequentialGroup() + .addComponent(scrollPanepane)); + + pack(); } -} \ No newline at end of file +} From 04a3dbbaccddb75a53f65b134d0fef41cd677e00 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Fri, 2 Nov 2018 00:44:45 +0300 Subject: [PATCH 2/5] Update ApplicationRunner.java --- src/editor/ApplicationRunner.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/editor/ApplicationRunner.java b/src/editor/ApplicationRunner.java index b7f538c..8ef14c9 100644 --- a/src/editor/ApplicationRunner.java +++ b/src/editor/ApplicationRunner.java @@ -2,6 +2,7 @@ public class ApplicationRunner { public static void main(String[] args) { - new TextEditor(); + TextEditor app = new TextEditor(); + app.setVisible(true); } } From db7328f22217c589e6c5d5fe791a7e99e73b4f37 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Fri, 2 Nov 2018 00:45:39 +0300 Subject: [PATCH 3/5] Update TextEditor.java --- src/editor/TextEditor.java | 104 +++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 26 deletions(-) diff --git a/src/editor/TextEditor.java b/src/editor/TextEditor.java index 1575f52..31226c9 100644 --- a/src/editor/TextEditor.java +++ b/src/editor/TextEditor.java @@ -3,34 +3,86 @@ import javax.swing.*; public class TextEditor extends JFrame { + private JTextField input = new JTextField("", 5); + private JButton loadbutton = new JButton("Load"); + private JButton savebutton = new JButton("Save"); + private JTextArea textArea = new JTextArea(); + private String filename = null; + + + public TextEditor() { - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); - setLayout(null); - setTitle("Text Editor"); - createTextArea(); - setSize(new Dimension(350, 300)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setLocationRelativeTo(null); - } + //устанавлиаем параметры основного окна + super("Text Editor"); + this.setSize(500, 400); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //устанавливаем размеры компонентов + + this.input.setSize(250, 30); + this.input.setLocation(20, 30); + this.loadbutton.setBounds(260, 30, 100, 30); + this.savebutton.setBounds(360, 30, 100, 30); + + //TextArea + + this.textArea.setLineWrap(true); + this.textArea.setWrapStyleWord(true); + + JScrollPane scrollPane = new JScrollPane(textArea); + scrollPane.setBounds(20, 60, 440, 280); + + //устанавливаем слушатель на кнопки и поля + + input.addActionListener(e -> { + this.filename = input.getText(); + }); + loadbutton.addActionListener(e -> { + File file = new File(this.filename.toLowerCase()); + + try { + Scanner scanner = new Scanner(file); + String text = ""; + while (scanner.hasNextLine()){ + String line = scanner.nextLine(); + text=text+ line+ "\n"; + } + + textArea.setText(text); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } + }); + savebutton.addActionListener(e -> { + + File file = new File(this.filename); + String alltext = this.textArea.getText(); + String [] strings = alltext.split("\n"); + + try { + FileWriter writer = new FileWriter(file); + for(int i = 0; i < strings.length; i ++){ + writer.write(strings[i]); + writer.write("\n"); + } + + writer.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + + }); + + //складываем все в контейнер + Container container = this.getContentPane(); + container.add(savebutton); + container.add(loadbutton); + container.add(input); + + this.getContentPane().add(scrollPane); + container.setLayout(new BorderLayout()); - private void createTextArea() { - JTextArea textArea = new JTextArea(); - JScrollPane scrollPanepane = new JScrollPane(textArea); - textArea.setLineWrap(true); - textArea.setWrapStyleWord(true); - Container pane = getContentPane(); - GroupLayout groupLayout = new GroupLayout(pane); - pane.setLayout(groupLayout); - groupLayout.setAutoCreateContainerGaps(true); - groupLayout.getAutoCreateGaps(); - - groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup() - .addComponent(scrollPanepane)); - groupLayout.setVerticalGroup(groupLayout.createSequentialGroup() - .addComponent(scrollPanepane)); - - pack(); } } + From aebbe4240bef7fd29794c77bb2278a7e599ebcd5 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Sat, 3 Nov 2018 20:02:30 +0300 Subject: [PATCH 4/5] Delete TextEditor.java --- src/editor/TextEditor.java | 88 -------------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 src/editor/TextEditor.java diff --git a/src/editor/TextEditor.java b/src/editor/TextEditor.java deleted file mode 100644 index 31226c9..0000000 --- a/src/editor/TextEditor.java +++ /dev/null @@ -1,88 +0,0 @@ -package editor; - -import javax.swing.*; - -public class TextEditor extends JFrame { - private JTextField input = new JTextField("", 5); - private JButton loadbutton = new JButton("Load"); - private JButton savebutton = new JButton("Save"); - private JTextArea textArea = new JTextArea(); - private String filename = null; - - - - public TextEditor() { - - //устанавлиаем параметры основного окна - super("Text Editor"); - this.setSize(500, 400); - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - //устанавливаем размеры компонентов - - this.input.setSize(250, 30); - this.input.setLocation(20, 30); - this.loadbutton.setBounds(260, 30, 100, 30); - this.savebutton.setBounds(360, 30, 100, 30); - - //TextArea - - this.textArea.setLineWrap(true); - this.textArea.setWrapStyleWord(true); - - JScrollPane scrollPane = new JScrollPane(textArea); - scrollPane.setBounds(20, 60, 440, 280); - - //устанавливаем слушатель на кнопки и поля - - input.addActionListener(e -> { - this.filename = input.getText(); - }); - loadbutton.addActionListener(e -> { - File file = new File(this.filename.toLowerCase()); - - try { - Scanner scanner = new Scanner(file); - String text = ""; - while (scanner.hasNextLine()){ - String line = scanner.nextLine(); - text=text+ line+ "\n"; - } - - textArea.setText(text); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } - }); - savebutton.addActionListener(e -> { - - File file = new File(this.filename); - String alltext = this.textArea.getText(); - String [] strings = alltext.split("\n"); - - try { - FileWriter writer = new FileWriter(file); - for(int i = 0; i < strings.length; i ++){ - writer.write(strings[i]); - writer.write("\n"); - } - - writer.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - - - }); - - //складываем все в контейнер - Container container = this.getContentPane(); - container.add(savebutton); - container.add(loadbutton); - container.add(input); - - this.getContentPane().add(scrollPane); - container.setLayout(new BorderLayout()); - - } -} - From b021e1612e32f0743dc39bff5a0b968b708deb68 Mon Sep 17 00:00:00 2001 From: AlinaOktyabrskaya <44409263+AlinaOktyabrskaya@users.noreply.github.com> Date: Sat, 3 Nov 2018 20:03:27 +0300 Subject: [PATCH 5/5] Create TextEditor.java --- src/editor/TextEditor.java | 137 +++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/editor/TextEditor.java diff --git a/src/editor/TextEditor.java b/src/editor/TextEditor.java new file mode 100644 index 0000000..60ff4aa --- /dev/null +++ b/src/editor/TextEditor.java @@ -0,0 +1,137 @@ +import java.awt.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; +import javax.swing.*; + + + +public class TextEditor extends JFrame { + private JTextField input = new JTextField("", 5); + private JButton loadbutton = new JButton("Load"); + private JButton savebutton = new JButton("Save"); + private JTextArea textArea = new JTextArea(); + private String filename = null; + private JMenuBar MenuBar; + private JPanel container; + + + + + public TextEditor(){ + + super("Text Editor"); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + this.textArea.setLineWrap(true); + this.textArea.setWrapStyleWord(true); + JScrollPane scrollPane = new JScrollPane(textArea); + + input.addActionListener(e -> { + this.filename = input.getText(); + }); + loadbutton.addActionListener(e -> { + loadFile(); + + }); + savebutton.addActionListener(e -> { + saveFile(); + }); + + + conteiner(); + + createMenu(); + + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + + panel.add(container, BorderLayout.PAGE_START); + panel.add(scrollPane, BorderLayout.CENTER); + + this.setMinimumSize(new Dimension(350,110)); + + this.getContentPane().add(panel); + + this.setJMenuBar(MenuBar); + this.setPreferredSize(new Dimension(350, 400)); + + this.pack(); + + + + } + + private void conteiner() { + this.container = new JPanel(); + + GridLayout gridLayout = new GridLayout(1,3,5,15); + container.setLayout(gridLayout); + + container.add(input); + + container.add(loadbutton); + container.add(savebutton); + container.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); + } + + private void createMenu() { + this.MenuBar = new JMenuBar(); + + JMenu menufile = new JMenu("File"); + MenuBar.add(menufile); + JMenuItem save = new JMenuItem("Save"); + menufile.add(save); + JMenuItem load = new JMenuItem("Load"); + menufile.add(load); + JMenuItem exit = new JMenuItem("Close"); + menufile.add(exit); + + + save.addActionListener(e -> { + saveFile(); + + }); + load.addActionListener(e -> { + loadFile(); + }); + exit.addActionListener(e -> { + this.dispose(); + }); + + } + + private void loadFile() { + File file = new File(this.filename.toLowerCase()); + try { + Scanner scanner = new Scanner(file); + String text = ""; + while (scanner.hasNextLine()){ + String line = scanner.nextLine(); + text=text+ line+ "\n"; + } + textArea.setText(text); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } + } + + private void saveFile() { + File file1 = new File(this.filename); + String alltext = this.textArea.getText(); + String [] strings = alltext.split("\n"); + try { + FileWriter writer = new FileWriter(file1); + for(int i = 0; i < strings.length; i ++){ + writer.write(strings[i]); + writer.write("\n"); + } + writer.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } +} +