java - GUI 不随 JTextArea 一起扩展

标签 java swing user-interface

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

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

enter image description here

这就是我的文本区域中多行文本(或只是按下几个“enter”键)的样子(与 GUI 配合不好)

enter image description here

我的代码:

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

参见How to Use Scroll Panes了解更多详情。

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

参见JTextArea constructor JTextArea(int rows, int columns)了解更多详情

关于java - GUI 不随 JTextArea 一起扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25925945/

相关文章:

java - 覆盖 MATLAB 默认静态 javaclasspath 的最佳方法

Java 扫雷 - ArrayIndexOutOfBounds 异常

java - 在 JScrollPane 中更新 JPanel 不起作用

user-interface - 页眉中的 Primefaces 按钮

user-interface - 在 GNU/Smalltalk 中开发 UI

Java Selenium Chromedriver.exe 不存在 IllegalStateException

使用简单的开关读取和写入 xml 或 json 的 Java 库

java - Scala/Swing - 响应同时发生的多个关键事件

java - 我需要有关“Deal or no Deal”Java 游戏的帮助

php - 无法找到 css 属性