Computer questions (Elementary)-How to Use arrays to implement notepad (Java)
Use java to implement a Notepad program and record the keys. The Code is as follows:
Package com. java. test; import java. awt. graphics; import java. awt. event. keyEvent; import java. awt. event. keyListener; import javax. swing. JFrame; import javax. swing. JPanel; public class Book {public static void main (String [] args) {JFrame jFrame = new JFrame (); jFrame. setSize (300,500); MyPanel myPanel = new MyPanel (); jFrame. add (myPanel); jFrame. addKeyListener (myPanel); myPanel. addKeyListener (myPanel); jFrame. show () ;}} class MyPanel extends JPanel implements KeyListener {char [] chars = new char [1000]; int size = 0; @ Overridepublic void paint (Graphics g) {super. paint (g); // clear the screen for (int I = 0; I <size; I ++) {g. drawString (chars [I] + "", 10 + 8 * I, 10) ;}@overridepublic void keyPressed (KeyEvent e) {chars [size] = e. getKeyChar (); size ++; repaint () ;}@ Overridepublic void keyReleased (KeyEvent e) {}@ Overridepublic void keyTyped (KeyEvent e ){}}
A character array is used to record all the characters that are pressed. The running example is as follows:
The Notebook function is implemented. The next section implements the cursor and deletion functions.