After learning Java events, I wrote an extremely simple notepad myself. MenuBar, Menu, MenuITem and other controls are used, and events include ActionListener and KeyListener.
The code is as follows:
package com.package3; /* * Function: Simple development of Notepad, you can save files, open files, and exit Notepad* author: ywq */ import javax.swing.*; import java.awt.event.*; import java.io.*; public class MenuText { //Define components: JFrame f; MenuBar mb; //Menu bar Menu mu; //Menu JTextArea jta; MenuItem openItem, saveItem, closeItem; //Submenu FileDialog openDia,saveDia; //Popular save and open box File file; //Constructor public MenuText() { //Calling the initialization function init(); } //Initialize the component public void init() { f=new JFrame("Simple Notepad"); mb=new MenuBar(); mu=new Menu("File"); openItem=new MenuItem("Open"); saveItem=new MenuItem("Save"); 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 attribute f.setBounds(200, 300, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); //Call event function event(); } //Event function to handle events public void event() { //Open option openItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Call openFile(); } }); //Save option saveItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Call the method to save the file. saveFile(); } }); //Add an event in the text area, that is, press Ctrl+S to save //Because there are many ways to listen for events on the keyboard, and we only need one of them, we can use the adapter KeyAdapter, //There is only one method to implement jta.addKeyListener(new KeyAdapter() { //Keypress method public void keyPressed(KeyEvent e){ if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S) { //Calling the method to save the file. saveFile(); //JOptionPane.showMessageDialog(null, "Yes"); } } }); //Close the option 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 display it//Get the path and file name String dirPath=openDia.getDirectory(); String fileName=openDia.getFile(); //Prevent clicks to cancel errors if(dirPath==null || fileName==null) return; jta.setText(""); //Clear the text area file=new File(dirPath,fileName); //Create a file object//Read the data according to the line and display it in the 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 determine whether the file exists if(file==null) { saveDia.setVisible(true); String dirPath = saveDia.getDirectory(); String fileName = saveDia.getFile(); //Prevent clicks to cancel errors if(dirPath==null || fileName==null) return; //Because the file does not exist. So you need to create a file object file = new File(dirPath,fileName); } //Write data to file try { BufferedWriter bw=new BufferedWriter(new FileWriter(file)); String info=jta.getText(); //Get the information of the text area bw.write(info); //Write operation bw.flush(); bw.close(); } catch (IOException e1) { throw new RuntimeException(); } } public static void main(String[] args) { //Create the object new MenuText(); } } }The operation results are shown in the figure:
The functions implemented by this program are:
(1) A file can be opened and edited.
(2) You can save the edited file.
(3) You can use Ctrl+S to save text
(4) You can click closeItem to exit the program.
When implementing function 3 , a KeyListener is added to the text area, and the adapter KeyAdapter is used to implement listening. But now when you need a combination listening, that is, the save operation will be triggered when both ctrl and S are pressed.
Regarding combined listening, the Java API provides corresponding methods.
Find the direct parent class of the KeyEvent class, namely the InputEvent class. As shown in the figure:
Check out the methods in the InputEvent class as follows:
As a subclass of the InputEvent class, the object e of the KeyEvent class can be directly called to the above method to make judgments. The isControlDown() method is used to determine whether the ctrl key is pressed. For example, if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S) in the program realizes combination judgment.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.