Java-如何在jtextarea中放大/缩放文本

发布于 2025-02-04 23:58:14 字数 1791 浏览 1 评论 0原文

我正在写记事本。我想在记事本中实现文本缩放。但是我不知道该怎么做。我正在尝试找到它,但每个人都建议更改字体尺寸。但是我需要另一个解决方案。

我正在创建新项目,并添加按钮和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);
    }

}


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);
    }

}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2025-02-11 23:58:14

简介

Oracle具有有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。密切注意在容器中放置组件部分。

我重新设计了你的gui。应用程序启动时的外观。我输入了一些文本,以便您可以看到字体更改。

我们缩小后的外观。

这是我们放大后的外观。

“在此处输入图像说明”

堆叠溢出缩放图像,因此文本正在放大并不那么明显。

解释

秋千设计用于与布局管理器。我创建了两个jpanels,一个用于jbuttons,一个用于jtextarea。我将jtextarea放在jscrollpane中,以便您可以键入10行。

我在int字段中跟踪字体大小。这是一个简单的应用程序模型。您的Swing应用程序应始终具有由一个或多个普通Java Getter/Setter类组成的应用程序模型。

代码

这是完整的可运行代码。

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;
    }

}

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.

enter image description here

Here's how it looks after we zoom out.

enter image description here

Here's how it looks after we zoom in.

enter image description here

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;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文