Teach you to write text editor of II with JAVA

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

Directory

This is Editor Chapter 1 JAVA Writing Text Editor (1), the need for students to understand 1 below.

In this chapter 1, we deal with the item event response in JMenuBar 1, starting with the simplest item.

Why say this is simple, because it needs to complete the function is very few, just come out a dialog box or window, and then display a text.

First of all, we write the code according to the order of executing the software. When we click menu, the subitem item will pop up. Then when we click item, a new window will pop up. Note: menu did the pop-up of item itself, but when we click item, there is no response, because we need to set up a listener for him to execute events:

Main window implements interface ActionListener


    public class test5 extends JFrame implements ActionListener{}

Want Override function actionPerformed


    @Override
    public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub
	}

In fact, we can also use ItemListener here to capture events generated by components with item, while ActionListener is the parent class of all listeners, which can listen to all events. Because we are worried that there will be other events to listen to, we can use ActionListener directly, which is also possible.

Next, we need to add listeners to item:


        /**
	 *  For all btn Follow item Unified 1 Set up the listener 
	 */
	public void initListener() {
		item_new.addActionListener(this);
		item_open.addActionListener(this);
		item_save.addActionListener(this);
		item_exit.addActionListener(this);
		item_undo.addActionListener(this);
		item_cut.addActionListener(this);
		item_copy.addActionListener(this);
		item_stick.addActionListener(this);
		item_delete.addActionListener(this);
		item_word_format.addActionListener(this);
		item_about.addActionListener(this);
	}

Constructor of the main window:


        public test5() {
		initMenuBar();
		initEditArea();
		initListener();
		
		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);
	}

After we add a listener to the control, we can successfully capture events (such as clicking, double clicking, left mouse button pressing, lifting, dragging, etc.), but where should we write about the handling methods of events? We need to write it in the abstract method of the interface.

Incidentally, an interface is a structure very similar to a class, with only variables and abstract methods. Much like abstract classes, neither can be instantiated (new). Both can be inherited (extends, implements), but the effect is different. The ultimate goal of abstract classes is to serve for instantiation, while interfaces are used to extract the common behavior of objects.

Let's add a processing event to item_about:


@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == item_about) {
			new about_Window();
		}
	}

OK, we have added listeners and events, events require a pop-up window (the window has a title, prompts, buttons), so we have to do a window class, and then put his new out.

We created a class in the package, and I posted the code directly below, because it is relatively simple here:


public class about_Window extends JFrame{
 
 private JButton btn_ok;
 private JLabel about_label;
 
 private JPanel panel ;
 private BoxLayout boxlayout;
 
 /**
  *  Constructor of window 
  */
 public about_Window() {
  panel = new JPanel();
  boxlayout = new BoxLayout(panel,BoxLayout.Y_AXIS);
  panel.setLayout(boxlayout);
  
  btn_ok = new JButton("OK");
  btn_ok.setAlignmentX(CENTER_ALIGNMENT);
  about_label = new JLabel(" Don't know how to use notepad? Look for me  ! ");
  about_label.setAlignmentX(CENTER_ALIGNMENT);
 
  
  panel.add(about_label);
  panel.add(btn_ok);
  
  
  this.add(panel);
  this.setSize(300,200);
  this.setTitle(" About ");
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  
  btn_ok.addActionListener(e->{
   this.dispose();
  });
 }
}

As you can see, we write directly in the constructor of the window, and then instantiate the window in the event monitoring of the main window, so that we can come out and click on the effect of opening the window.

Summarize the following chapter: 1. Analyze the steps of software from the user's point of view, click item and pop up a window, and then write the code according to this idea. 2. Handle events by inheriting interfaces, adding listeners and rewriting listener methods. 3. Rewrite 1 window and wait for the instantiation of the main window.

Summarize


Related articles: