English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Content
After learning Java events, I wrote a very simple notepad myself. It uses MenuBar, Menu, MenuITem, and other controls, and events include ActionListener and KeyListener.
The code is as follows:3import java.io. /* * package com.package * Function: Development of a simple notepad, which can save files, open files, and exit the notepad */ author: ywq*import java.io. import javax.swing.*import java.io. import java.awt.*import java.io. import java.awt.event.*import java.io. ; //public class MenuText { Define components: JFrame f; //MenuBar mb; Menu mu; //Menu JTextArea jta; MenuItem openItem, saveItem, closeItem; //Submenu FileDialog openDia,saveDia; //Pop-up save and open boxes File file; //Constructor public MenuText() { //Call the initialization function init(); } //Perform initialization operations on the components public void init() { f=new JFrame("简易记事本"); mb=new MenuBar(); mu=new Menu("文件"); openItem=new MenuItem("打开"); saveItem=new MenuItem("保存"); closeItem=new MenuItem("Exit"); jta=new JTextArea(); f.add(jta); //Add mu.add(openItem); mu.add(saveItem); mu.add(closeItem); mb.add(mu); f.setMenuBar(mb); openDia=new FileDialog(f, "Open", FileDialog.LOAD); saveDia=new FileDialog(f, "Save", FileDialog.SAVE); //Set JFrame properties f.setBounds(200, 300, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); //Call the event function event(); } //Event function, to handle the event public void event() { //Open options openItem.addActionListener(new ActionListener()) { @Override public void actionPerformed(ActionEvent e) { //Call the method to open the file openFile(); } }); //Save options saveItem.addActionListener(new ActionListener()) { @Override public void actionPerformed(ActionEvent e) { //Call the method to save the file. saveFile(); } }); //Add an event to the text area, that is, press Ctrl+S can be saved //Because there are many ways to listen to keyboard events, and we only need one of them, we can use the adapter KeyAdapter, //Thus, only one method needs to be implemented jta.addKeyListener(new KeyAdapter()) { //Key press method public void keyPressed(KeyEvent e){ if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S) { //Call the method to save the file. saveFile(); //JOptionPane.showMessageDialog(null, "Right!"); } } }); //Close options closeItem.addActionListener(new ActionListener()) { @Override public void actionPerformed(ActionEvent e) { //Exit the system System.exit(0); } }); } //method to open text public void openFile() { openDia.setVisible(true); //set it to be displayed //get the path and file name String dirPath = openDia.getDirectory(); String fileName = openDia.getFile(); //prevent error reporting when cancel is clicked if(dirPath == null || fileName == null) return ; jta.setText(""); //clear the text area file = new File(dirPath, fileName); //create a file object //read data line by line, display in text area try { BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; while((line = br.readLine()) != null) { jta.append(line+"\r\n"); } br.close(); } catch (IOException ex) { throw new RuntimeException("read failed"); } } //method to save text. public void saveFile() { //first check if the file exists if(file == null) { saveDia.setVisible(true); String dirPath = saveDia.getDirectory(); String fileName = saveDia.getFile(); //prevent error reporting when cancel is clicked if(dirPath == null || fileName == null) return ; //Since the file does not exist, a file object needs to be created file = new File(dirPath, fileName); } //write data to file try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); String info = jta.getText(); //get the text area information bw.write(info); //writing operation bw.flush(); bw.close(); } catch (IOException e1) { throw new RuntimeException(); } } public static void main(String[] args) { //Create an object new MenuText(); } }
The running result is shown in the figure below:
The functions implemented by this program include:
(1) can open a file and can edit it.
(2) can save the edited file.
(3) can use Ctrl+) save the text with S
(4) you can click closeItem to exit the program.
InImplement the function3When adding a KeyListener to the text area, it uses the adapter KeyAdapter to implement the listening. But now what is needed is a combination listening, that is, when both Ctrl and S are pressed, the save operation will be triggered.
The Java API provides the corresponding method for combination listening.
Find the direct superclass of the KeyEvent class, which is the InputEvent class. As shown in the figure:
Check the methods in the InputEvent class, as shown below:
The object e of the KeyEvent class, which is a subclass of the InputEvent class, can directly call the above method to make a judgment. The isControlDown() method is used to determine whether the control key is pressed. As shown in the program if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S) the combination judgment is implemented.
That's all for this article. Hope it helps everyone's learning and also hope everyone will support the Niyao tutorial.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright. It has not been edited by humans and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.