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/3] 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/3] 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/3] 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(); } } +