Import Java. AWT. borderlayout; import Java. AWT. container; import Java. AWT. event. keyevent; import Java. AWT. event. keylistener; import javax. swing. jframe; import javax. swing. jtextarea; import javax. swing. text. badlocationexception; public class indentation extends jframe implements keylistener {public static final int width = 1000; public static final int Height = 700; jtextarea ta; Public indentation () {initialize ();} public void initialize () {TA = new jtextarea (); TA. addkeylistener (this); Container c = This. getcontentpane (); C. setlayout (New borderlayout (); C. add (TA, borderlayout. center); setbounds (100,100, width, height); this. setdefaclocloseoperation (exit_on_close); setvisible (true);} public static void main (string ARGs []) {New indentation ();} public void keypressed (keyevent E) {// in eclipse, it is implemented through tab. However, when pressing the tab key in the jtextarea control, it is processed as the '\ t' character, so use the CTRL + q shortcut to implement if (E. iscontroldown () & keyevent. vk_q = E. getkeycode () {// indent string text = TA to the right of multiple rows. gettext (); int start = TA. getselectionstart (); int end = TA. getselectionend (); int startrow = 0; int endrow = 0; int n = 0; try {startrow = TA. getlineofoffset (start); endrow = TA. getlineofoffset (end); // n = endrow-startrow; while (endrow> = startrow) {end = text. substring (0, end ). lastindexof (10); TA. insert ("\ t", end + 1); // TA. replacerange ("\ t", end, end + 1); endrow -- ;}} catch (badlocationexception E1) {e1.printstacktrace () ;}} else if (E. isshiftdown () & keyevent. vk_tab = E. getkeycode () {// unindent int start = TA on the left of multiple rows. getselectionstart (); int end = TA. getselectionend (); int startrow = 0; int endrow = 0; int n = 0; try {startrow = TA. getlineofoffset (start); endrow = TA. getlineofoffset (end); // n = endrow-startrow; while (endrow> = startrow) {end = TA. gettext (). substring (0, end ). lastindexof (10); If (TA. gettext (). charat (end + 1) = '\ t') {TA. replacerange ("", end + 1, end + 2);} endrow --;} catch (badlocationexception E1) {// todo auto-generated catch blocke1.printstacktrace ();}}} public void keyreleased (keyevent arg0) {} public void keytyped (keyevent arg0 ){}}