【问题标题】:How to use a text area?如何使用文本区域?
【发布时间】:2025-02-27 05:20:02
【问题描述】:

我正在用 java 创建一个 GUI,并想使用 JTextArea,但是我在将它添加到框架时遇到了很多麻烦。我将如何创建一个文本区域,然后使用它来读取文本或显示文本?

到目前为止,这是我的 GUI 代码:

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

public class addMemoUI extends JFrame { 

JFrame frame = new JFrame();

/**
 * Create the application.
 */
public addMemoUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frame.getContentPane().setBackground(new Color(255, 255, 255));
    frame.getContentPane().setLayout(null);

    JButton button = new JButton("Create");
    button.setBackground(new Color(100, 149, 237));
    button.setBounds(135, 350, 130, 50);
    frame.getContentPane().add(button);

    JLabel lblMemos = new JLabel("MEMOS");
    lblMemos.setForeground(new Color(100, 149, 237));
    lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
    lblMemos.setBounds(22, 21, 234, 37);
    frame.getContentPane().add(lblMemos);

    JButton button_1 = new JButton("Cancel");
    button_1.setBackground(new Color(100, 149, 237));
    button_1.setBounds(5, 350, 130, 50);
    frame.getContentPane().add(button_1);

    frame.setBounds(100, 100, 270, 400);
    frame.setUndecorated(true); //REMOVES MENU BAR
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton btnExit = new JButton("");
    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
}

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

非常感谢:)

【问题讨论】:

  • 快速说明:您既是从JFrame 扩展而来,又是使用变量JFrame frame。您可能只想要两者之一。
  • 谢谢,我会修改的:)
  • 要对您的问题进行排序,您应该使用Layout Manager。使用空布局是个坏主意(代码更长、可靠性更低、可扩展性更低......)。一旦您知道如何使用布局,您就会知道如何将JTextArea 添加到框架/面板。
  • @DSquare +1。 Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。

标签: java swing user-interface textarea layout-manager


【解决方案1】:

这里是如何使用 JTextArea 的示例。您可以设置、获取或附加文本。你可以通过谷歌找到其他人。

public class Example {
private JTextArea jtextbox;

private void initialize() {
    JFrame frm = new JFrame();
      :
    JScrollPane scroll = new JScrollPane();

    jtextbox= new JTextArea();
    scroll.setViewportView(jtextbox); // add scroll panel
    jtextbox.setTabSize(4);
    jtextbox.setLineWrap(true);
    jtextbox.setBackground(SystemColor.window);
}

private void setText(String text)  {

    jtextbox.append(text); // or setText(text)
}

private String getText()  {
    return jtextbox.getText();
}

}

【讨论】:

  • 非常感谢,我如何将文本框实际添加到框架中,因为目前它没有出现。
  • 如何将它添加到框架中?
  • 在 Eclipse 上使用 WindowBuilder 插件。这是简单的方法。您可以在 JPanel 上添加组件并需要决定 Layouts。这是你的选择。
  • 我在 eclipse 中设计这个。
  • 您安装了 WindowBuilder 吗?如果没有,这里是 URL eclipse.org/windowbuilder/download.php。你只需要拖放。然后检查包含绘图信息的 initialize() 函数。不要忘记每个布局以不同的方式显示它们。我认为默认值是 GridLayOut。祝你好运