简体   繁体   中英

Java - how to zoom in/zoom out text in JTextArea

I am writing in a notepad. And I want to implement text scaling in my notepad. But I don't know how to do it. I'm trying to find it but everyone is suggesting to change the font size. But I need another solution.

I am create new project and add buttons and JTextArea.

package zoomtest;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class zoom {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    zoom window = new zoom();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public zoom() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);
        
        JButton ZoomIn = new JButton("Zoom in");
        ZoomIn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //Code here...
            }
        });
        panel.add(ZoomIn);
        
        JButton Zoomout = new JButton("Zoom out");
        Zoomout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //Code here...
            }
        });
        panel.add(Zoomout);
        
        JTextArea jta = new JTextArea();
        frame.getContentPane().add(jta, BorderLayout.CENTER);
    }

}


Introduction

Oracle has a helpful tutorial, Creating a GUI With Swing . Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.

I reworked your GUI. Here's how it looks when the application starts. I typed some text so you can see the font change.

在此处输入图像描述

Here's how it looks after we zoom out.

在此处输入图像描述

Here's how it looks after we zoom in.

在此处输入图像描述

Stack Overflow scales the images, so it's not as obvious that the text is zooming.

Explanation

Swing was designed to be used with layout managers . I created two JPanels , one for the JButtons and one for the JTextArea . I put the JTextArea in a JScrollPane so you could type more than 10 lines.

I keep track of the font size in an int field. This is a simple application model. Your Swing application should always have an application model made up of one or more plain Java getter/setter classes.

Code

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ZoomTextExample {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new ZoomTextExample();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private int pointSize;

    private Font textFont;

    private JFrame frame;

    private JTextArea jta;

    private JTextField pointSizeField;

    public ZoomTextExample() {
        this.pointSize = 16;
        this.textFont = new Font(Font.DIALOG, Font.PLAIN, pointSize);
        initialize();
    }

    private void initialize() {
        frame = new JFrame("Text Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createButtonPanel(), BorderLayout.NORTH);
        frame.add(createTextAreaPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        JButton zoomIn = new JButton("Zoom in");
        zoomIn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                incrementPointSize(+2);
                updatePanels();
            }
        });
        panel.add(zoomIn);

        panel.add(Box.createHorizontalStrut(20));

        JLabel label = new JLabel("Current font size:");
        panel.add(label);

        pointSizeField = new JTextField(3);
        pointSizeField.setEditable(false);
        pointSizeField.setText(Integer.toString(pointSize));
        panel.add(pointSizeField);

        panel.add(Box.createHorizontalStrut(20));

        JButton zoomOut = new JButton("Zoom out");
        zoomOut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                incrementPointSize(-2);
                updatePanels();
            }
        });
        panel.add(zoomOut);

        return panel;
    }

    private JPanel createTextAreaPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        jta = new JTextArea(10, 40);
        jta.setFont(textFont);
        JScrollPane scrollPane = new JScrollPane(jta);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private void updatePanels() {
        pointSizeField.setText(Integer.toString(pointSize));
        textFont = textFont.deriveFont((float) pointSize);
        jta.setFont(textFont);
        frame.pack();
    }

    private void incrementPointSize(int increment) {
        pointSize += increment;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM