java - JTextArea 作为控制台

标签 java swing console console-application jtextarea

我在下面发布了两段代码。两个代码单独工作都很好。现在,当我运行 Easy 文件并单击“开始”按钮时,我希望实现 AddNumber 类。我的意思是说,除了在控制台上运行 AddNumber 之外,有什么方法可以让 AddNumber 在单击“开始”按钮后在我在第一个类中创建的 JTextArea 中运行吗?我想也许是通过 Action 监听器?(就像我们在按钮的情况下所做的那样)但我不确定。有没有其他方法可以让我的 JTextArea 充当其他 .java 文件的控制台?

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


public class Easy extends JFrame{

    JTextArea text=new JTextArea();
    JPanel panel=new JPanel(new GridLayout(2,2));

    JButton button1 =new JButton("Start");

    public Easy(){

        panel.add(text);

        panel.add(button1);
        add(panel,BorderLayout.CENTER);

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae){
            //add code to call the other class and make the JTextArea act as a console
            }
        });
   }

   public static void main(String arg[]){
       Easy frame=new Easy();
       frame.setSize(300,100);
       frame.setVisible(true);
   }
}

第二课:

import java.util.Scanner;

class AddNumber
{
    public static void main(String args[])
    {
        int x, y, z;
        System.out.println("Enter two numbers to be added ");
        Scanner in = new Scanner(System.in);
        x = in.nextInt();
        y = in.nextInt();
        z = x + y;
        System.out.println("Sum of entered numbers = "+z);
    }
}

我看过一些关于 PrintStream 的帖子..但我认为这不适用于这里。 请帮帮我。谢谢:)

更新:我找到了这个链接:http://www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1它的工作原理是它显示“输入要添加的两个数字”...但是用户可以在哪里提供他的输入?

编辑:我只需要在类的 main 方法中引用控制台...并且它可以工作...好吧,并不完全如我所愿...但部分...输入仍然必须来自 IDE 的终端..

最佳答案

如果您在 Google 上搜索:“stdout JTextArea”,您将看到几个链接来解决您的问题。

在最后一个链接中,buddybob 扩展了 java.io.OutputStream 以将标准输出打印到他的 JTextArea。我在下面列出了他的解决方案。

TextAreaOutputStream.java

/*
*
* @(#) TextAreaOutputStream.java
*
*/

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author  Ranganath Kini
* @see      javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
    private JTextArea textControl;

    /**
     * Creates a new instance of TextAreaOutputStream which writes
     * to the specified instance of javax.swing.JTextArea control.
     *
     * @param control   A reference to the javax.swing.JTextArea
     *                  control to which the output must be redirected
     *                  to.
     */
    public TextAreaOutputStream( JTextArea control ) {
        textControl = control;
    }

    /**
     * Writes the specified byte as a character to the
     * javax.swing.JTextArea.
     *
     * @param   b   The byte to be written as character to the
     *              JTextArea.
     */
    public void write( int b ) throws IOException {
        // append the data as characters to the JTextArea control
        textControl.append( String.valueOf( ( char )b ) );
    }  
}

The TextAreaOutputStream extends the java.io.OutputStream class and overrides its write(int) method overload, this class uses a reference to a javax.swing.JTextArea control instance and then appends output to it whenever its write( int b ) method is called.

To use the TextAreaOutputStream class, [yo]u should use:

用法

// Create an instance of javax.swing.JTextArea control
JTextArea txtConsole = new JTextArea();

// Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
// PrintStream around it to support the println/printf methods.
PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) );

// redirect standard output stream to the TextAreaOutputStream
System.setOut( out );

// redirect standard error stream to the TextAreaOutputStream
System.setErr( out );

// now test the mechanism
System.out.println( "Hello World" );

关于java - JTextArea 作为控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48638502/

相关文章:

java h2内存数据库错误: Table not found

java - 如何在 Google OR 工具的 VRP 中强制执行硬约束,某些节点不应首先和最后访问

java - OpenCSV 解析不显示特殊字符

linux - 启动-停止-重启 shell 脚本的功能

使用子进程的python脚本,将所有输出重定向到文件

java - 如何使用Java Midi STOP

java - 解包 JPOS ISO 8583 时解包字段 44 时出现问题

java - 不显示在 GridLayout 的 JPanel 中

java - 让java显示超过1个.add()

未使用 Java 类