From 94f24b9232aeb6de3cbb2bf118d9ef8460221941 Mon Sep 17 00:00:00 2001 From: fsb4000 Date: Thu, 28 Feb 2019 22:52:41 +0700 Subject: [PATCH 1/3] first stage --- .idea/misc.xml | 6 --- .idea/modules.xml | 8 ---- .idea/project.iml | 10 ----- Java/Swing/src/editor/ApplicationRunner.java | 14 +++++++ Java/Swing/src/editor/TextEditor.java | 42 ++++++++++++++++++++ src/editor/ApplicationRunner.java | 7 ---- src/editor/TextEditor.java | 11 ----- 7 files changed, 56 insertions(+), 42 deletions(-) delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/project.iml create mode 100644 Java/Swing/src/editor/ApplicationRunner.java create mode 100644 Java/Swing/src/editor/TextEditor.java delete mode 100644 src/editor/ApplicationRunner.java delete mode 100644 src/editor/TextEditor.java diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index a165cb3..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index a0733a5..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/project.iml b/.idea/project.iml deleted file mode 100644 index 47baa8c..0000000 --- a/.idea/project.iml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/Java/Swing/src/editor/ApplicationRunner.java b/Java/Swing/src/editor/ApplicationRunner.java new file mode 100644 index 0000000..43373df --- /dev/null +++ b/Java/Swing/src/editor/ApplicationRunner.java @@ -0,0 +1,14 @@ +package editor; + +import javax.swing.*; + +public class ApplicationRunner { + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new TextEditor(); + } + }); + } +} diff --git a/Java/Swing/src/editor/TextEditor.java b/Java/Swing/src/editor/TextEditor.java new file mode 100644 index 0000000..7965688 --- /dev/null +++ b/Java/Swing/src/editor/TextEditor.java @@ -0,0 +1,42 @@ +package editor; + +import javax.swing.*; +import javax.swing.border.Border; +import java.awt.*; + +public class TextEditor extends JFrame { + private JTextArea textArea; + public TextEditor() { + setupMainWindowParameters(); + createWidgets(); + setupStyle(); + setupLayout(); + } + + private void setupMainWindowParameters() { + setTitle("The first stage"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setSize(375, 375); + setVisible(true); + } + + private void setupLayout() { + add(textArea); + } + + private void createWidgets() { + textArea = new JTextArea(); + } + + private void setupStyle() { + /***************************TextArea**************************************/ + Border border = BorderFactory. + createMatteBorder(32, 32,32,32, + new Color(237,237,237)); + textArea.setBorder(border); + Font font = textArea.getFont(); + float size = font.getSize() + 2.0f; + textArea.setFont( font.deriveFont(size) ); + /***************************TextArea**************************************/ + } +} \ No newline at end of file diff --git a/src/editor/ApplicationRunner.java b/src/editor/ApplicationRunner.java deleted file mode 100644 index b7f538c..0000000 --- a/src/editor/ApplicationRunner.java +++ /dev/null @@ -1,7 +0,0 @@ -package editor; - -public class ApplicationRunner { - public static void main(String[] args) { - new TextEditor(); - } -} diff --git a/src/editor/TextEditor.java b/src/editor/TextEditor.java deleted file mode 100644 index 228c84b..0000000 --- a/src/editor/TextEditor.java +++ /dev/null @@ -1,11 +0,0 @@ -package editor; - -import javax.swing.*; - -public class TextEditor extends JFrame { - public TextEditor() { - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setSize(300, 300); - setVisible(true); - } -} \ No newline at end of file From 58e89c9b9672a32135c1cf54797ff6df59d6d8e5 Mon Sep 17 00:00:00 2001 From: fsb4000 Date: Sat, 2 Mar 2019 06:24:54 +0700 Subject: [PATCH 2/3] stage two --- Java/Swing/src/editor/ApplicationRunner.java | 6 +- Java/Swing/src/editor/TextEditor.java | 96 +++++++++++++++++--- 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/Java/Swing/src/editor/ApplicationRunner.java b/Java/Swing/src/editor/ApplicationRunner.java index 43373df..88de767 100644 --- a/Java/Swing/src/editor/ApplicationRunner.java +++ b/Java/Swing/src/editor/ApplicationRunner.java @@ -1,9 +1,11 @@ +// This is a personal academic project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com package editor; import javax.swing.*; -public class ApplicationRunner { - public static void main(String[] args) { +class ApplicationRunner { + public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { diff --git a/Java/Swing/src/editor/TextEditor.java b/Java/Swing/src/editor/TextEditor.java index 7965688..93675a3 100644 --- a/Java/Swing/src/editor/TextEditor.java +++ b/Java/Swing/src/editor/TextEditor.java @@ -1,42 +1,112 @@ +// This is a personal academic project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com package editor; import javax.swing.*; -import javax.swing.border.Border; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; public class TextEditor extends JFrame { private JTextArea textArea; + private JTextField fileName; + private JButton save; + private JButton load; + private JPanel main; + private JScrollPane textScroll; + public TextEditor() { setupMainWindowParameters(); createWidgets(); setupStyle(); setupLayout(); + setupActions(); + } + + private void setupActions() { + save.addActionListener(new ActionListener() { + @Override + public void actionPerformed(final ActionEvent e) { + File file = new File(fileName.getText()); + try { + PrintWriter printWriter = new PrintWriter(file); + printWriter.println(textArea.getText()); + printWriter.close(); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } + } + }); + + load.addActionListener(new ActionListener() { + @Override + public void actionPerformed(final ActionEvent e) { + byte[] encoded = null; + try { + encoded = Files.readAllBytes(Paths.get(fileName.getText())); + } catch (IOException e1) { + e1.printStackTrace(); + } + textArea.setText(new String(encoded, StandardCharsets.UTF_8)); + } + }); } private void setupMainWindowParameters() { - setTitle("The first stage"); + setTitle("Text Editor"); setDefaultCloseOperation(EXIT_ON_CLOSE); - setSize(375, 375); setVisible(true); } private void setupLayout() { - add(textArea); + main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); + + JPanel buttons = new JPanel(); + buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); + buttons.add(fileName); + buttons.add(Box.createRigidArea(new Dimension(12, 0))); + buttons.add(save); + buttons.add(Box.createRigidArea(new Dimension(12, 0))); + buttons.add(load); + + JPanel textPanel = new JPanel(); + textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS)); + textPanel.add(textScroll); + + main.add(buttons); + main.add(Box.createRigidArea(new Dimension(0, 12))); + main.add(textPanel); + + add(main); + pack(); } private void createWidgets() { - textArea = new JTextArea(); + main = new JPanel(); + textArea = new JTextArea(20, 10); + fileName = new JTextField(30); + save = new JButton("Save"); + load = new JButton("Load"); + textScroll = new JScrollPane(textArea); } private void setupStyle() { - /***************************TextArea**************************************/ - Border border = BorderFactory. - createMatteBorder(32, 32,32,32, - new Color(237,237,237)); - textArea.setBorder(border); + main.setBorder(BorderFactory.createEmptyBorder(12,12,12,12)); + Font font = textArea.getFont(); float size = font.getSize() + 2.0f; - textArea.setFont( font.deriveFont(size) ); - /***************************TextArea**************************************/ + textArea.setFont(font.deriveFont(size)); + + textScroll.setHorizontalScrollBarPolicy( + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); + textScroll.setVerticalScrollBarPolicy( + ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); } -} \ No newline at end of file +} From 47a8a10daadce607625682b06560b877f6fec072 Mon Sep 17 00:00:00 2001 From: fsb4000 Date: Sat, 2 Mar 2019 20:58:21 +0700 Subject: [PATCH 3/3] stage three --- Java/Swing/src/editor/TextEditor.java | 84 ++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/Java/Swing/src/editor/TextEditor.java b/Java/Swing/src/editor/TextEditor.java index 93675a3..a2f15c0 100644 --- a/Java/Swing/src/editor/TextEditor.java +++ b/Java/Swing/src/editor/TextEditor.java @@ -5,7 +5,6 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -20,18 +19,44 @@ public class TextEditor extends JFrame { private JButton save; private JButton load; private JPanel main; + private JPanel buttons; private JScrollPane textScroll; + private AbstractAction saveAction; + private AbstractAction loadAction; + private AbstractAction exitAction; + private JMenuBar menuBar; + private JMenuItem loadMenuItem; + private JMenuItem saveMenuItem; + private JMenuItem exitMenuItem; public TextEditor() { - setupMainWindowParameters(); createWidgets(); - setupStyle(); + createActions(); setupLayout(); setupActions(); + setupMenu(); + setupStyle(); + setupMainWindowParameters(); } - private void setupActions() { - save.addActionListener(new ActionListener() { + private void setupMenu() { + JMenu file = new JMenu("File"); + + file.add(loadMenuItem); + file.add(saveMenuItem); + + file.addSeparator(); + + file.add(exitMenuItem); + + menuBar.add(file); + } + + private void createActions() { + saveAction = new AbstractAction() { + { + putValue(NAME, "Save"); + } @Override public void actionPerformed(final ActionEvent e) { File file = new File(fileName.getText()); @@ -43,9 +68,12 @@ public void actionPerformed(final ActionEvent e) { e1.printStackTrace(); } } - }); + }; - load.addActionListener(new ActionListener() { + loadAction = new AbstractAction() { + { + putValue(NAME, "Load"); + } @Override public void actionPerformed(final ActionEvent e) { byte[] encoded = null; @@ -56,19 +84,39 @@ public void actionPerformed(final ActionEvent e) { } textArea.setText(new String(encoded, StandardCharsets.UTF_8)); } - }); + }; + + exitAction = new AbstractAction() { + { + putValue(NAME, "Exit"); + } + @Override + public void actionPerformed(final ActionEvent e) { + dispose(); + } + }; + } + + private void setupActions() { + save.addActionListener(saveAction); + load.addActionListener(loadAction); + + saveMenuItem.setAction(saveAction); + loadMenuItem.setAction(loadAction); + exitMenuItem.setAction(exitAction); } private void setupMainWindowParameters() { setTitle("Text Editor"); setDefaultCloseOperation(EXIT_ON_CLOSE); + setJMenuBar(menuBar); + setSize(400,400); setVisible(true); } private void setupLayout() { main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); - JPanel buttons = new JPanel(); buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); buttons.add(fileName); buttons.add(Box.createRigidArea(new Dimension(12, 0))); @@ -85,16 +133,20 @@ private void setupLayout() { main.add(textPanel); add(main); - pack(); } private void createWidgets() { main = new JPanel(); - textArea = new JTextArea(20, 10); + buttons = new JPanel(); + textArea = new JTextArea(10, 10); fileName = new JTextField(30); save = new JButton("Save"); load = new JButton("Load"); textScroll = new JScrollPane(textArea); + menuBar = new JMenuBar(); + loadMenuItem = new JMenuItem(); + saveMenuItem = new JMenuItem(); + exitMenuItem = new JMenuItem(); } private void setupStyle() { @@ -104,9 +156,11 @@ private void setupStyle() { float size = font.getSize() + 2.0f; textArea.setFont(font.deriveFont(size)); - textScroll.setHorizontalScrollBarPolicy( - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); - textScroll.setVerticalScrollBarPolicy( - ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + Dimension maxSize = fileName.getPreferredSize(); + fileName.setMaximumSize(maxSize); + + buttons.add(Box.createGlue()); + buttons.setMaximumSize(new Dimension(buttons.getMaximumSize().width, + buttons.getPreferredSize().height)); } }