javase implementation Notepad

java programming is distributed, micro service? What else can I write without Spring
I don't know when I like to write interface programs and app s in java. Maybe that's all the fun programmers have left. But for me. I enjoy the process. Programming ape, love programming. Leisure time, a cup of coffee, a piece of light music, open the notebook with a line of code to achieve their own ideas, is really a pleasure.
Effect

Code

package example;
 
/**
* ┏┓   ┏┓
*┏┛┻━━━ ┻┓
*┃       ┃  
*┃   ━   ┃
*┃ ┳┛ ┗┳ ┃
*┃       ┃
*┃   ┻   ┃
*┃       ┃
*┗━┓   ┏━┛
*  ┃   ┃Gods and beasts bless you
*  ┃   ┃No BUG in code!
*  ┃   ┗━━━┓
*  ┃       ┣┓
*  ┃       ┏┛
*  ┗┓┓┏━┳┓┏┛
*   ┃┫┫ ┃┫┫
*   ┗┻┛ ┗┻┛             
*
*!!!!!!!!!!!!!!!!!!Get busy living or get busy dying!!!!!!!!!!!!!       
*/
 
/**
 * Notepad startup class
 *
 * @author www.javayihao.top
 * @Time 2019
 */
public class App {
    public static void main(String[] args) {
        // Launch custom window object
        EditorFrame editor = new EditorFrame();
    }
}
 
package example;
 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.text.DefaultStyledDocument;
 
/**
 * java Implement the main program of Notepad program
 *
 * @author Administrator
 *
 */
/*
 * Custom window class EditorFrame inherits JFrame to implement action listener interface ActionListener
 */
public class EditorFrame extends JFrame implements ActionListener {
 
    // Define related properties
    private JMenuBar menuBar;// Top menu bar
    private JMenu menuFile, menuEdit, menuAbout;// Menu files, editing, about
    // Menu item new, open, save, exit, cut, copy, paste, about
    private JMenuItem itemNewFile, itemOpen, itemSave, itemExit, itemCut, itemCopy, itemPaste, itemAbout;
    private JToolBar toolBar;// Toolbar
    // Buttons new, open, save, exit, cut, copy, paste, about
    private JButton butNewFile, butOpen, butSave, butExit, butCut, butCopy, butPaste, butAbout;
    private JTextPane textPane;// Edit window
    private JLabel label;// Bottom tab bar
    private JFileChooser fileChooser;// File selector
    private JScrollPane scrollPane;
    /*
     * Construction method
     */
 
    public EditorFrame() {
        // Instantiate menu bar
        menuBar = new JMenuBar();
        // Instantiate menu
        menuFile = new JMenu("file");
        menuEdit = new JMenu("edit");
        menuAbout = new JMenu("about");
        // Instantiate menu items and add event listening
        itemNewFile = new JMenuItem("Newly build");
        itemNewFile.addActionListener(this);// Set up monitoring
        itemNewFile.setActionCommand("newFile");
        itemOpen = new JMenuItem("open");
        itemOpen.addActionListener(this);// Set up monitoring
        itemOpen.setActionCommand("open");
        itemSave = new JMenuItem("Preservation");
        itemSave.addActionListener(this);// Set up monitoring
        itemSave.setActionCommand("save");
        itemExit = new JMenuItem("Sign out");
        itemExit.addActionListener(this);// Set up monitoring
        itemExit.setActionCommand("exit");
        itemCut = new JMenuItem("shear");
        itemCut.addActionListener(this);// Set up monitoring
        itemCut.setActionCommand("cut");
        itemCopy = new JMenuItem("copy");
        itemCopy.addActionListener(this);// Set up monitoring
        itemCopy.setActionCommand("copy");
        itemPaste = new JMenuItem("paste");
        itemPaste.addActionListener(this);// Set up monitoring
        itemPaste.setActionCommand("paste");
        itemAbout = new JMenuItem("about");
        itemAbout.addActionListener(this);// Set up monitoring
        itemAbout.setActionCommand("about");
        // File settings menu item
        menuFile.add(itemNewFile);
        menuFile.add(itemOpen);
        menuFile.add(itemExit);
        menuFile.add(itemSave);
        // Edit settings menu item
        menuEdit.add(itemCut);
        menuEdit.add(itemCopy);
        menuEdit.add(itemPaste);
        // About settings menu
        menuAbout.add(itemAbout);
        // Menu bar settings menu
        menuBar.add(menuFile);
        menuBar.add(menuEdit);
        menuBar.add(menuAbout);
        this.setJMenuBar(menuBar);
        // Instantiate toolbar
        toolBar = new JToolBar();
        // Instantiate button
        butNewFile = new JButton("Newly build");
        butNewFile.addActionListener(this);
        butNewFile.setActionCommand("newFile");
        butOpen = new JButton("open");
        butOpen.addActionListener(this);// Set up monitoring
        butOpen.setActionCommand("open");
        butSave = new JButton("Preservation");
        butSave.addActionListener(this);// Set up monitoring
        butSave.setActionCommand("save");
        butExit = new JButton("Sign out");
        butExit.addActionListener(this);// Set up monitoring
        butExit.setActionCommand("exit");
        butCut = new JButton("shear");
        butCut.addActionListener(this);// Set up monitoring
        butCut.setActionCommand("cut");
        butCopy = new JButton("copy");
        butCopy.addActionListener(this);// Set up monitoring
        butCopy.setActionCommand("copy");
        butPaste = new JButton("paste");
        butPaste.addActionListener(this);// Set up monitoring
        butPaste.setActionCommand("paste");
        butAbout = new JButton("about");
        butAbout.addActionListener(this);// Set up monitoring
        butAbout.setActionCommand("about");
        // Toolbar settings button
        toolBar.add(butNewFile);
        toolBar.add(butOpen);
        toolBar.add(butSave);
        toolBar.add(butExit);
        toolBar.add(butCut);
        toolBar.add(butCopy);
        toolBar.add(butPaste);
        toolBar.add(butAbout);
 
        // Instantiate edit window
        textPane = new JTextPane();
        label = new JLabel("www.javayihao.top ----by xiaoyuan");
 
        // Instantiate file selector
        fileChooser = new JFileChooser();
        // Instantiate scroll bar
        scrollPane = new JScrollPane(textPane);
        // Add components to the window container (use boundary layout)
        Container container = getContentPane(); // Get container
        container.add(toolBar, BorderLayout.NORTH); // Add toolbar
        container.add(scrollPane, BorderLayout.CENTER);
        container.add(label, BorderLayout.SOUTH); // Add status bar
 
        // Initialize window
        this.setTitle("Ape notebook");// Window title
        this.setSize(600, 300);// Form size
        this.setIconImage((new ImageIcon("images/logo.png")).getImage());// Set icon
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Set to shut down process
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;// Get screen width
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;// Get screen height
        this.setLocation((width - 500) / 2, (height - 400) / 2);// Play shows
        this.setVisible(true);// Set form visible
        this.setResizable(true);// Can change form size
    }
 
    /*
     * Event method
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("newFile")) {
            textPane.setDocument(new DefaultStyledDocument());// Empty documents
        } else if (e.getActionCommand().equals("open")) {
            int i = fileChooser.showOpenDialog(EditorFrame.this); // Show open file dialog
            if (i == JFileChooser.APPROVE_OPTION) { // Click on the dialog box to open the options
                File f = fileChooser.getSelectedFile(); // Selected files
                try {
                    InputStream is = new FileInputStream(f); // Get file input stream
                    textPane.read(is, "d"); // Read in file to text pane
                } catch (Exception ex) {
                    ex.printStackTrace(); // Output error message
                }
            }
        } else if (e.getActionCommand().equals("save")) {
            int i = fileChooser.showSaveDialog(EditorFrame.this); // Show save file dialog
            if (i == JFileChooser.APPROVE_OPTION) { // Click the Save button in the dialog box
                File f = fileChooser.getSelectedFile(); // Selected files
                try {
                    FileOutputStream out = new FileOutputStream(f); // Get file output stream
                    out.write(textPane.getText().getBytes()); // File out
                } catch (Exception ex) {
                    ex.printStackTrace(); // Output error message
                }
            }
        } else if (e.getActionCommand().equals("exit")) {
            System.exit(0);
        } else if (e.getActionCommand().equals("cut")) {
            textPane.cut();// Call text cut method
        } else if (e.getActionCommand().equals("copy")) {
            textPane.copy();// Call copy method
        } else if (e.getActionCommand().equals("paste")) {
            textPane.paste();// Call paste method
        } else if (e.getActionCommand().equals("about")) {
            JOptionPane.showMessageDialog(EditorFrame.this, "www.javayihao.top---Simple text editor demonstration");
        }
    }
 
}
 

Well, it's been more than an hour. It's very comfortable to write and play. The disadvantage of the code is that there are too many duplicate codes. Originally, I planned to customize an event array and use the Stream stream feature in java8 to add events to components. In order to see clearly, that's all
More java actual project articles focus on WeChat public number java No. 1

Keywords: Java Programming Spring

Added by davidohuf on Thu, 07 Nov 2019 01:09:08 +0200