XazGreatXia 2024-07-23 16:08 采纳率: 31%
浏览 1

Java JTextArea问题

package ChatExtends;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

public class ChatWindow extends JFrame{

    public String username;
    
    public void load() throws Exception{

        this.setSize(1000,800);

        this.setName("ChatWindow");

        this.setResizable(false);

        Container ct = this.getContentPane();

        File mimage = new File("images/mlogo.png");

        Image mainimage = ImageIO.read(mimage);

        this.setIconImage(mainimage);

        JPanel buttom = new JPanel();

        buttom.setSize(1000,100);

        buttom.setLayout(new BorderLayout());

        JTextArea inputtext = new JTextArea(1,80);

        JTextArea MessageArea = new JTextArea();

        inputtext.setLineWrap(true);

        inputtext.setWrapStyleWord(true);

        inputtext.setTabSize(2);

        inputtext.setPreferredSize(new Dimension(700,100));

        //inputtext.setFont(new Font("Arial",Font.BOLD,15));

        Border b = BorderFactory.createLineBorder(Color.BLACK);

        inputtext.setBorder(b);

        inputtext.setTabSize(2);

        inputtext.setFont(new Font("Arial",Font.BOLD,20));

        buttom.add(inputtext,BorderLayout.WEST);

        JButton SendButton = new JButton("Send");

        //SendButton.setPreferredSize(new Dimension(1000-inputtext.WIDTH,100));

        Font S = new Font("Arail",Font.ITALIC,50);

        SendButton.setFont(S);

        SendButton.setFocusable(false);

        SendButton.setBorder(b);

        SendButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                //throw new UnsupportedOperationException("Unimplemented method 'actionPerformed'");
                String out = inputtext.getText().trim();
                
                MessageArea.append(out);

                MessageArea.paintImmediately(MessageArea.getBounds());

                inputtext.setText("");
            }

        });

        buttom.add(SendButton,BorderLayout.EAST);   

        ct.add(buttom,"South");
        
        JPanel Top = new JPanel();

        JLabel name = new JLabel(username);

        name.setHorizontalAlignment(SwingConstants.CENTER);

        name.setVerticalAlignment(SwingConstants.CENTER);

        Font t = new Font("Arial",Font.ITALIC,50);

        name.setFont(t);

        Top.add(name);

        ct.add(Top,"North");

        JPanel Center = new JPanel();

        MessageArea.setEditable(false);

        MessageArea.setBounds(0,300,1000,700);

        MessageArea.setPreferredSize(new Dimension(1000,500));

        MessageArea.setBackground(Color.white);

        MessageArea.setForeground(Color.black);

        MessageArea.setFocusable(false);

        Center.add(MessageArea);

        ct.add(Center,"Center");

        this.setVisible(true);

    }

}


请问能不能在放大字体的时候不让JTextArea的大小自动放大?

  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-07-23 16:08
    关注

    以下内容由AIGC及阿里嘎多学长共同生成、有用望采纳:


    在Java Swing中,JTextArea组件的字体大小改变确实可能会影响其显示的大小,因为Swing组件通常会根据其内容和字体大小来调整其大小。但是,你可以通过设置JTextAreasetPreferredSize()来指定一个固定的尺寸,这样即使字体大小改变,组件的尺寸也不会自动调整。

    你已经正确地指出了如何设置字体和固定尺寸的方法。以下是一些额外的建议和注意事项:

    1. 设置字体:你已经知道如何设置字体,这是通过调用setFont()方法实现的。

    2. 固定尺寸:通过调用setPreferredSize()方法来设置组件的首选尺寸。这将确保组件的尺寸不会根据内容或字体大小的变化而变化。

    3. 重新验证和重绘:在更改字体或尺寸设置后,调用revalidate()repaint()方法以确保组件正确地重新绘制并应用新的设置。

    4. 注意布局管理器:如果你使用的布局管理器(如BorderLayout)对组件的尺寸有特定的要求,你可能需要调整布局管理器的设置或使用其他布局管理器。

    5. 滚动条:如果你的JTextArea内容超出了其固定尺寸,你可能需要添加滚动条来允许用户查看所有内容。这可以通过设置setLineWrap(true)setWrapStyleWord(true)来实现,同时使用JScrollPane来包裹JTextArea

    6. 性能考虑:如果你在字体大小和组件尺寸之间进行频繁的更改,可能需要考虑性能影响。revalidate()repaint()可能会导致额外的绘制和布局计算。

    7. 错误修正:在你的代码中,SendButton的字体设置使用了错误的字体名称"Arail",应该是"Arial"

    8. 代码优化:可以考虑将字体和尺寸的设置放入方法中,以便于管理和复用。

    通过这些步骤,你应该能够实现在放大字体时不让JTextArea的大小自动放大的需求。如果你遇到任何具体的问题或错误,请随时提问,我会尽力帮助你解决。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月23日

悬赏问题

  • ¥15 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题
  • ¥15 python的EOFError该怎么解决?
  • ¥15 Fluent,液体进入旋转区域体积分数不连续
  • ¥15 java linux下将docx文件转pdf
  • ¥15 maven无法下载依赖包
  • ¥15 关于pycharm, Callable[[str],bool]作为方法参数使用时, lambda 类型不提示问题
  • ¥15 layui数据重载无效
  • ¥15 寻找了解qq家园纵横四海的程序猿。