Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a text Editor with JAVA

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to write a text editor with JAVA". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write a text editor with JAVA.

Preface

It's written here in swing awt.

We're probably going to make an editor like a computer notepad. You can adjust the font, font size, color. You can open and save files, create a new window, click exit, and there is a window for introduction. As a whole, it doesn't seem to have a lot of functions, as long as part of it is done.

Analysis of ideas:

First of all, our goal model is the notepad of the computer. We can see the structure by opening the editor.

1. The title is displayed in the title of the window.

2. The following line is a toolbar with files, editing, formatting, viewing, and help. Here we choose the file, format, help to do, the general situation is about the same.

3. Below is a TextArea with a super high screen share, which is also characterized by a scroll bar.

File menu bar: there is a new window, open the file, save the file, and an exit button. The difficulty is mainly in opening and saving files. Why? Because when we click the open and save buttons, a file selection Dialog will pop up. Do you want to draw this thing yourself? if you want to draw it yourself, it will be quite troublesome. You have to traverse the disk and paste it in the window or something. Fortunately, we have a packaged tool, JFileChooser.

Third, format menu: a pop-up window, with various forms of text attribute selection, the middle of a display of the text area, the following two buttons, to determine that is to save the format, modify the properties of the main window. Cancel to close the window without changing the properties.

Fourth, help menu: this is the simplest, because there is only one pop-up window to display text, so we are going to start here.

OK, after analyzing each part, it feels much clearer. Next, let's take the first step to draw the main window.

Concrete realization

Draw the main window and build the menu bar according to their respective relationships: JMenuBar,JMenu and JMenuItem are used here. As the name implies, one is the menu bar, one is the menu, and one is the menu item. Let's look at the code:

Public class test5 extends JFrame {private JMenuBar menuBar; / / menu bar private JMenu menu_File,menu_Edit,menu_Help,menu_Format; / / menu bar private JMenuItem item_new,item_open,item_save,item_exit; / / for subitems of the file menu private JMenuItem item_undo,item_cut,item_copy,item_stick,item_delete / / for edit menu sub-item private JMenuItem item_about; / / for help menu sub-item private JMenuItem item_word_format; public test5 () {initMenuBar (); this.setJMenuBar (menuBar); this.setSize (800600) This.setTitle (Custom text Editor); this.setVisible (true); this.setLocationRelativeTo (null); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) Initialize menubar. Here are some small shortcut settings * you can consider setting all components * two forms: * menu_File.setMnemonic ('f') For menu * item_word_format.setAccelerator (KeyStroke.getKeyStroke); for item * / public void initMenuBar () {menuBar = new JMenuBar (); for menu_File = new JMenu ("file (F)"); for menu_File.setMnemonic ('f') / / f+alt Open item_new = new JMenuItem ("New"); item_open = new JMenuItem ("Open"); item_save = new JMenuItem ("Save"); item_exit = new JMenuItem ("exit"); menu_File.add (item_new); menu_File.add (item_open) Menu_File.add (item_save); menu_File.add (item_exit); / / File menu menu_Edit = new JMenu ("Edit (E)"); menu_Edit.setMnemonic ('e'); item_undo = new JMenuItem ("undo") Item_cut = new JMenuItem ("cut"); item_copy = new JMenuItem ("copy"); item_stick = new JMenuItem ("paste"); item_delete = new JMenuItem ("delete"); menu_Edit.add (item_undo); menu_Edit.add (item_cut) Menu_Edit.add (item_copy); menu_Edit.add (item_stick); menu_Edit.add (item_delete); / / Edit menu menu_Help = new JMenu ("help (H)"); menu_Help.setMnemonic ('h') Item_about = new JMenuItem (about); menu_Help.add (item_about); / / Help menu menu_Format = new JMenu ("format (O)"); menu_Format.setMnemonic ('o'); item_word_format = new JMenuItem ("font (F)") Item_word_format.setAccelerator (KeyStroke.getKeyStroke); / / add shortcut keys to item: menu_Format.add (item_word_format); menuBar.add (menu_File); menuBar.add (menu_Edit); menuBar.add (menu_Format) MenuBar.add (menu_Help);} public static void main (String [] args) {test5 T5 = new test5 ();}}

Next up is the text editing area under the menu bar, where all you need is a JTextArea, and don't forget the JScrollPane bar:

Public class test5 extends JFrame {...

...

Private static JTextArea edit_text_area; / / private JTextArea edit_text_area; / / Editing area private JScrollPane scroll_bar; / / adding edit_text_area to the scrollable pane can become a scrollable text box. JScrollPane is a pane, and you can set the direction public test5 () {initMenuBar () InitEditArea (); this.setJMenuBar (menuBar); this.setSize (800600); this.add (scroll_bar); this.setTitle (Custom text Editor); this.setVisible (true) This.setLocationRelativeTo (null); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);} / * * initialize the editing area * install textarea with scrollpane * while setting the direction of pane * / public void initEditArea () {edit_text_area = new JTextArea () Scroll_bar = new JScrollPane (edit_text_area); scroll_bar.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);} public void initMenuBar () {...} public static void main (String [] args) {test5 T5 = new test5 () }} at this point, I believe you have a deeper understanding of "how to write a text editor with JAVA". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report