Teach you to write text editor of with JAVA

  • 2021-12-12 04:25:35
  • OfStack

Thinking analysis of catalogue preface: summary of concrete realization

Preface

It is written here in swing and awt.

We will probably make an editor like a notebook of a computer. You can adjust font, font size and color. Can open, save files, create a new window, click Exit, and have a window about introduction. It seems that there are not many functions as a whole, so long as one part and one part are completed, it will be easy to do.

Thinking analysis:

1. First of all, our target model is the Notepad of the computer. Open the editor to see the structure:

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

2. The following line is a toolbar with files, editing, formatting, viewing and help. Here we will choose files, formats and help, and the general situation is similar.

3. Next, there is an TextArea with a high screen proportion. He also has a scroll bar.

2. File menu bar: There are new windows, open files, save files, and an exit button. The main difficulty lies in opening and saving files. Why? Because when we click the Open and Save buttons, Dialog will pop up for 1 file selection. This thing is not to draw, if you want to draw, it is quite troublesome, to traverse the disk, and then paste it into the window or something. Fortunately, we have a packaged tool JFileChooser.

3. Format menu: Pop up a window with various forms of text attribute selection, a text area for display in the middle, and the following two buttons to save the format and modify the attributes of the main window. Cancel to close the window without changing the properties.

4. Help menu: This is the simplest, because there is only one pop-up window for displaying text, so we plan to start here.

OK, after analyzing each part for 1 time, the feeling is much clearer. Next, let's go to step 1 and 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, 1 is a menu bar, 1 is a menu item. Look at the code below:


public class test5 extends JFrame{
 
	private JMenuBar menuBar;
	// Menu bar 
	private JMenu menu_File,menu_Edit,menu_Help,menu_Format;
	// Menu in menu bar 
	private JMenuItem item_new,item_open,item_save,item_exit;
	// For file Subitems of menu 
	private JMenuItem item_undo,item_cut,item_copy,item_stick,item_delete;
	// For edit Subitems of menu 
	private JMenuItem item_about;
	// For help Subitems of menu 
	private JMenuItem item_word_format;
	
	
	public test5() {
		initMenuBar();
		
		this.setJMenuBar(menuBar);
		this.setSize(800,600);
		this.setTitle(" Custom text editor ");
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 *  Right menubar To initialize, there are 1 Some small shortcut settings 
	 *  You can consider setting for all components 1 Under 
	 *  Two forms: 
	 * menu_File.setMnemonic('f');  Right menu
	 * item_word_format.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,false));  Right item
	 */
	public void initMenuBar() {
		menuBar = new JMenuBar();
		menu_File = new JMenu(" Documents (F)");
		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(" Quit ");
		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(" Revoke ");
		item_cut = new JMenuItem(" Shear ");
		item_copy = new JMenuItem(" Duplicate ");
		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('F',java.awt.Event.CTRL_MASK,false));// To item Add shortcut keys 
		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 is the text editing area below the menu bar, where only one JTextArea is needed, and don't forget the scroll bar (JScrollPane):


public class test5 extends JFrame{
 
	...

...


 
	private static JTextArea edit_text_area;
	//private JTextArea edit_text_area;
	
	// Edit region 
	private JScrollPane scroll_bar;
	// Scrollable pane  Inside add edit_text_area You can become 1 A text box that can be scrolled, JScrollPane Yes 1 A pane And you can set the direction at the same time 
	
	public test5() {
		initMenuBar();
		initEditArea();
		
		this.setJMenuBar(menuBar);
		this.setSize(800,600);
		
		this.add(scroll_bar);
		
		this.setTitle(" Custom text editor ");
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	
	/**
	 *  Initialize the edit area 
	 *  Use scrollpane Install textarea
	 *  Simultaneous pair pane Set direction 
	 */
	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();
	}
	
	
 
}

In order to save space, I will delete the repeated code and replace it with... I believe it will not affect everyone's viewing effect.

OK, now the whole main window is out. We review what we have done in this article: 1. Organize the thinking of 1, and analyze the components according to the function and style of Notepad. 2. Initialize the menu bar. 3. Initialize the text editing area.

Now we are going to start doing event response.

Summarize


Related articles: