简体   繁体   English

如何使用文字区域?

[英]How to use a text area?

I m creating a GUI in java and would like to use a JTextArea , however I am having a lot of trouble adding it to the frame. 我正在用Java创建GUI并想使用JTextArea ,但是将其添加到框架时遇到了很多麻烦。 How would I go about creating a text Area and then using it to read text or display text? 我将如何创建一个文本区域,然后使用它来阅读文本或显示文本?

Here is my GUI code so far: 到目前为止,这是我的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();
            }
        }
    });
}
}

Thanks very much :) 非常感谢 :)

Here is example for how to use JTextArea. 这是有关如何使用JTextArea的示例。 You can set, get or append text. 您可以设置,获取或附加文本。 You can find the others by google. 您可以通过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();
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM