Computer questions (Elementary)-How to Use arrays to implement notepad (Java)

Source: Internet
Author: User

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.
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.