java - 如何使用文本区域?

标签 java swing user-interface textarea layout-manager

我正在用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();
            }
        }
    });
}
}

非常感谢:)

最佳答案

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

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

}

关于java - 如何使用文本区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22819353/

相关文章:

java - Spring Boot Rest API-无效请求参数异常 : java. lang.IllegalArgumentException:参数值 [计算机] 与预期类型不匹配

java - JFrame 中的背景图像

JQuery UI - 选项卡(# 和本地文档链接)+两个 Accordion - 当其他选项卡重新加载时不显示第一个 Accordion

java - XPath 转换为 JSoup 元素

java - 使用 Apache 的 HttpClient 与使用 JDK 的 URLConnection 从 applet 中连接到 URL

java - Java 中的拼接语言解释器

java - JTable 中的动态搜索过滤器

java - J面板: layout help needed

jquery - 使用JQuery .click 和.toggle 收缩和扩大同心圆

user-interface - wxpython 角落中的 UI