In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to write a text editor with JAVA". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Why this is simple, because it needs to complete very few functions, just come out a dialog box or window, and then display a text.
First of all, we write the code in the order in which the software is executed. When we click menu, the sub-item item pops up. Then when we click on item, a new window pops up. Note: the pop-up item is done by menu itself, but there is no response when we click item, because we need to set up a listener for him to execute the event:
Main window implements interface ActionListener
Public class test5 extends JFrame implements ActionListener {}
Want the Override function actionPerformed
@ Override public void actionPerformed (ActionEvent arg0) {/ / TODO Auto-generated method stub}
In fact, we can also use ItemListener to capture events generated by components with item, and ActionListener is the parent class of all listeners, so you can listen to all events. Because you are worried that there will be other events to listen to, you can use ActionListener directly.
Next we need to add listeners to the item:
/ * set listeners for all btn and item * / 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 for the main window:
Public test5 () {initMenuBar (); initEditArea (); initListener (); 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);}
After we add a listener to the control, we can successfully capture events (such as clicking, double-clicking, pressing down the left mouse button, lifting, dragging, etc.), but where do we write about the handling of events? We need to write it in the abstract method of the interface.
By the way: interfaces are structures that are very similar to classes, 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 the abstract class is to serve for instantiation, while the interface is used to extract the common behavior of the object.
Let's add a handling 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, which require a pop-up window (window with title, prompt, button), so we need to make a window class and new it out.
Let's create a class in the package, and I'll paste the code directly, because it's relatively simple here:
Public class about_Window extends JFrame {private JButton btn_ok; private JLabel about_label; private JPanel panel; private BoxLayout boxlayout; / * * window constructor * / 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 ("can't use notepad? Find me!); about_label.setAlignmentX (CENTER_ALIGNMENT); panel.add (about_label); panel.add (btn_ok); this.add (panel); this.setSize (300200); this.setTitle (about); this.setVisible (true); this.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); btn_ok.addActionListener (e-> {this.dispose ();}) This is the end of "how to write a text Editor with JAVA". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.