English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article shares the specific code for the swing jtextArea scrollbar and text zoom with everyone for reference, the specific content is as follows
When a jtextArea with a scrollbar is added scrolling events such as zooming with Ctrl+When the scrolling and zooming events are added at the same time during wheeling, how can we ensure that these two events occur simultaneously without interfering with each other? That is, the text does not scroll up and down when the scrollbar zooms in and out.
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 Zoom"); 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 scrolling event jsp.removeMouseWheelListener(sysWheel);//Remove system scrolling, add when needed 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, the scroll is zoomed in and out Font f = jta.getFont(); if(e.getWheelRotation()<0){//If the scrollbar scrolls forward, the text is enlarged jta.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize())+1)); }//The scrollbar scrolls back to shrink the text jta.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize())-1)); } }//If the ctrl key is not pressed, it is a system scroll jsp.addMouseWheelListener(sysWheel); sysWheel.mouseWheelMoved(e);//Trigger the system scroll event. jsp.removeMouseWheelListener(sysWheel); } } } public static void main(String[] args){ new jtaWheel(); } }
That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the Yelling Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)