This article shares the specific code of swing jtextArea scrollbar and text scaling for your reference. The specific content is as follows
When the jtextArea with a scroll bar is added with a scroll event such as scaling ctrl+wheel, the added scroll event and scroll zoom event will coincide. How to make these two events occur simultaneously without interfering with each other? That is, the text will not scroll up and down when the scroll bar is enlarged and reduced.
import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; public class jtaWheel { JFrame jf; JTextArea jta; JScrollPane jsp; MouseWheelListener sysWheel; public jtaWheel(){ jf = new JFrame("Scroll Scaling"); jf.setBounds(500,500,600,400); jta = new JTextArea(); jsp = new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); sysWheel = jsp.getMouseWheelListeners()[0];//Get system scroll event jsp.removeMouseWheelListener(sysWheel);//Remove system scrolling, add jsp.addMouseWheelListener(new event()); jf.add(jsp); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } private class event extends MouseAdapter{ @Override public void mouseWheelMoved(MouseWheelEvent e){ if(e.isControlDown()){//When the ctrl key is pressed, scrolling is to zoom in and out Font f = jta.getFont(); if(e.getWheelRotation()<0){// If the scroll bar is forward, enlarge the text jta.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize()+1)); }else if(e.getWheelRotation()>0){//The scroll bar shrinks the text backwards jta.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize()-1)); } }else{//When ctrl is not pressed, it scrolls for the system jsp.addMouseWheelListener(sysWheel); sysWheel.mouseWheelMoved(e);//Triggers the system scroll event. jsp.removeMouseWheelListener(sysWheel); } } public static void main(String[] args){ new jtaWheel(); } }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.