java - 如何收听 JTextArea

标签 java swing user-interface listener jtextarea

我正在创建一个个人 Java 终端,我需要它来做一些我现在不太理解的事情。我需要程序监听 JTextArea 中输入的内容,同时还知道程序将始终显示

[USERNAME]@[OPERATING SYSTEM]:~$

当按下“Enter”键时。我还需要程序将上述部分设置为不可编辑,并允许在永久声明后设置字符输入。 如果有人能够帮助我解决这个问题,我的程序如下,然后是监听器的代码,这很可能需要大量编辑。

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

import java.io.*;

public class testGUI extends JFrame {
      boolean _active = true;
     String _username = System.getProperty("user.name").toLowerCase();
     String _os = System.getProperty("os.name").trim().toLowerCase();
     JTextArea _commandLine = new JTextArea(_username + "@" + _os + ":~$ ");

    public testGUI() {
        super("Java Terminal");

        setSize(800,600);
        setLocation(100,100);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        GUISetup();

        setVisible(true);
    }

    public void GUISetup() {
        add(_commandLine);
        _commandLine.addActionListener(new CommandLineListener());
    }


        public static void main(String[] args) {
        new testGUI();
    }
}   

监听器代码如下。

        try {
        while(_active) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print(_username + "@" + _os + ":~$ ");
            String command = br.readLine();
                if(command.equals("cf")) {
                    new commandCreateFile();
                } else if(command.equals("cof")) {
                    new commandCompile();
                } else if(command.equals("help")) {
                    System.out.println("Commands");
                    System.out.println(" cf              - Creates .java files, does not compile.");
                    System.out.println(" ccf             - Creates .java files, compiles on creation.");
                    System.out.println(" help            - Shows help documentation.");
                } else if(command.equals("exit")) {
                    System.out.print("Are you sure you want to exit? (Y/N) ");
                    String exit = br.readLine();
                    if(exit.equalsIgnoreCase("y")) {
                    System.exit(0);
                    }
                } else if(command.isEmpty()) {
                    // do nothing.
                } else {
                    System.out.println("\"" + command + "\" does not exist. Please review the \"help\" menu");
                }
        }
    } catch(Exception ex) {
        System.out.println("There was a problem: " + ex);
    }

最佳答案

使用附加到JTextArea的 DocumentDocumentListener和附加到DocumentDocumentFilter code> 检查允许进行哪些编辑。

关于java - 如何收听 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9545209/

相关文章:

java - classLoader 无法加载实现此模块之外的类的类

java - 使用 XPath 循环 XML 字符串 - Java

java - 我的 KeyListener 无法读取按键输入

java - 正确调整 JScrollPane 及其内容的大小

java - 在按钮上方的地方?这叫什么?

java - 鼠标光标在对象上滚动时不会改变

java - Eclipse:搜索特定作者的java文件

java - JTextArea 背景的填充百分比

.net - 可以创建带有文本框和标签的 ToolStripMenuItem 吗?

java - 为什么编译器会在多行中输出单个警告?