Java GUI不随JTextArea一起扩展

Java GUI不随JTextArea一起扩展,java,swing,user-interface,Java,Swing,User Interface,我正在尝试用java创建一个字符计数器,我的代码的核心部分已经开始工作了。但是,我的GUI有问题。当我在JTextArea字段中按enter键时,它会展开它,但它不会同时展开我的GUI。这导致我的文本字段垂直扩展到覆盖所有按钮和字符计数的位置。如何使GUI与JTextArea一起扩展?谢谢:) 注意:对于图片的超链接,我深表歉意,我还是这个网站的新手,所以我没有足够的声誉上传图片 这是我的文本区域中一行文本的外观(与GUI配合良好) 在我的文本区域中有多行文本(或只按几下“回车”键)时,情况就

我正在尝试用java创建一个字符计数器,我的代码的核心部分已经开始工作了。但是,我的GUI有问题。当我在JTextArea字段中按enter键时,它会展开它,但它不会同时展开我的GUI。这导致我的文本字段垂直扩展到覆盖所有按钮和字符计数的位置。如何使GUI与JTextArea一起扩展?谢谢:)

注意:对于图片的超链接,我深表歉意,我还是这个网站的新手,所以我没有足够的声誉上传图片

这是我的文本区域中一行文本的外观(与GUI配合良好)

在我的文本区域中有多行文本(或只按几下“回车”键)时,情况就是这样(与GUI不兼容)

我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class characterCounterTwov4{
    //creates a new Jframe to put our frame objets in
    JFrame frame = new JFrame();

    //creates a text field JTextArea frame object
    JTextArea txtField = new JTextArea();

    //stores the string of the the jtextfield into a variable text
    String text = txtField.getText();

    //creates a text field that is uneditable with the word "characters"
    String charString = "Characters: ";
    JTextField charField = new JTextField(charString, 25);

    //string that will be used in a text field to display the # of chars
    String charCount = Integer.toString(text.length());

    //Text field that displays charCount
    JTextField charFieldTwo = new JTextField(charCount, 10);    

public characterCounterTwov4(){

    //disables the charField from being editable.
    charField.setEditable(false);

    //calculate button
    JButton calcButton = new JButton("Calculate");
    calcButton.addActionListener(new ActionListener(){
        public void actionPerformed(java.awt.event.ActionEvent event)
         {
            System.out.println("button pressed");

            //stores the string of the the jtextfield into a variable text
            text = txtField.getText();

            //string that will be used in a text field to display the # of chars
            String charCount = Integer.toString(text.length());

            //Text field that displays charCount
            charFieldTwo.setText(charCount);
         }
    });

    /*******************/
    /*   Frame Setup   */
    /*******************/

    //sets the layout of the frame
    frame.setLayout(new BorderLayout());

    //add's elements to the frame
    frame.add(txtField, BorderLayout.NORTH);
    frame.add(charField, BorderLayout.CENTER);
    frame.add(charFieldTwo, BorderLayout.SOUTH);
    frame.add(calcButton, BorderLayout.EAST);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

public static void main(String[] args){
    new characterCounterTwov4();
    System.out.println("End of program. Should not get here");
}
}

JTextArea
放入
JScrollPane

有关更多详细信息,请参阅

通过
JTextArea
的构造函数指定行和列的大小,这将允许您在滚动窗格干预和添加滚动条之前指定所需的文本区域的可视区域

有关更多详细信息,请参阅