In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Java how to achieve the text compiler", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java how to achieve the text compiler" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Brief introduction
A simple text compiler that opens a file through a pop-up dialog box, writes it, and saves it.
Code
Package Code.a;import java.io.*;import javax.swing.*;import java.awt.event.*;public class Editor implements ActionListener {JFrame f; JTextArea ta; JScrollPane sp; JMenuBar menubar; JMenu menu; JMenuItem openItem,saveItem,closeItem,exitItem; FileReader fr; FileWriter fw; JFileChooser fc; File file; / / construct the interface, and this class is used as a listening class Public Editor () {f = new JFrame ("simple text editor"); ta = new JTextArea (); sp = new JScrollPane (ta); / / Roller f.add (sp); menubar = new JMenuBar (); / / menu bar menu = new JMenu ("File"); openItem = new JMenuItem ("Open") / / four options saveItem = new JMenuItem ("Save"); closeItem = new JMenuItem ("Close"); exitItem = new JMenuItem ("Exit"); menu.add (openItem); menu.add (saveItem); menu.add (closeItem); menu.add (exitItem); menubar.add (menu); f.setJMenuBar (menubar); f.setSize (400300) F.setVisible (true); / / four option event functions OpenItem.addActionListener (this); saveItem.addActionListener (this); closeItem.addActionListener (this); exitItem.addActionListener (this);} public void actionPerformed (ActionEvent e) {/ / exception try {/ / when the open option is clicked, the open file dialog box pops up If (e.getActionCommand () = = "Open") {fc = new JFileChooser (); if (fc.showOpenDialog (null) = = JFileChooser.APPROVE_OPTION) {file = fc.getSelectedFile (); fr = new FileReader (file); int c While ((c = fr.read ())! =-1) {ta.append (String.valueOf ((char) c));} fr.close () When you click the Save option, the Save File dialog box pops up. Else if (e.getActionCommand () = = "Save") {String str = ta.getText (); fc = new JFileChooser (); if (fc.showSaveDialog (null) = = JFileChooser.APPROVE_OPTION) {file = fc.getSelectedFile (); fw = new FileWriter (file) Fw.write (str); fw.close ();}} / / clear the text box when you click the close option Else if (e.getActionCommand () = = "Close") {ta.setText (");} / / exit the current interface when you click to exit the option; else if (e.getActionCommand () = =" Exit ") {System.exit (0);}} / / handles the exception Catch (IOException exp) {exp.printStackTrace ();}} public static void main (String [] args) {Editor e = new Editor () }} here, the article "how to implement a text compiler in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
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.