JTextArea 作为控制台

时间:2023-01-12
本文介绍了JTextArea 作为控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

import java.awt.*;导入 javax.swing.*;导入 java.awt.event.*;公共类 Easy 扩展 JFrame{JTextArea 文本=新 JTextArea();JPanel 面板=新 JPanel(新 GridLayout(2,2));JButton button1 =new JButton("开始");公共容易(){panel.add(文本);panel.add(button1);添加(面板,BorderLayout.CENTER);button1.addActionListener(new ActionListener() {公共无效actionPerformed(ActionEvent ae){//添加代码以调用其他类并使 JTextArea 充当控制台}});}公共静态无效主要(字符串arg []){简易框架=新简易();frame.setSize(300,100);frame.setVisible(true);}}

第二课:

导入 java.util.Scanner;类 AddNumber{公共静态无效主(字符串参数 []){整数 x, y, z;System.out.println("输入两个要相加的数字");扫描仪输入 = 新扫描仪(System.in);x = in.nextInt();y = in.nextInt();z = x + y;System.out.println("输入数字的总和 = "+z);}}

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

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

我只需要在我的班级的主要方法中引用控制台......它工作......好吧,不完全像我希望的那样......但部分......输入还是要从IDE的终端去..

解决方案

如果你在 Google 上搜索:stdout JTextArea",你会找到几个链接来解决你的问题.

  • http://www.coderanch.com/t/458147/GUI/java/Redirect-output-stderr-stdout-JTextArea
  • 将 System.out 重定向到 JTextPane
  • http://www.jcreator.com/forums/index.php?showtopic=773

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

TextAreaOutputStream.java

<代码>/*** @(#) TextAreaOutputStream.java**/导入 java.io.IOException;导入 java.io.OutputStream;导入 javax.swing.JTextArea;/*** 将其输出写入 javax.swing.JTextArea 的输出流* 控制.** @作者兰加纳特·基尼* @see javax.swing.JTextArea*/公共类 TextAreaOutputStream 扩展 OutputStream {私有 JTextArea 文本控件;/*** 创建一个 TextAreaOutputStream 的新实例,它写入* 到 javax.swing.JTextArea 控件的指定实例.** @param control 对 javax.swing.JTextArea 的引用* 必须将输出重定向到的控件*                  到.*/公共TextAreaOutputStream(JTextArea控件){文本控制 = 控制;}/*** 将指定字节作为字符写入* javax.swing.JTextArea.** @param b 要作为字符写入的字节* JTextArea.*/公共无效写入(int b)抛出 IOException {//将数据作为字符附加到 JTextArea 控件textControl.append(String.valueOf((char)b));}}

<块引用>

TextAreaOutputStream 扩展了 java.io.OutputStream 类并覆盖其 write(int) 方法重载,此类使用引用 javax.swing.JTextArea 控件实例,然后每当调用它的 write( int b ) 方法时,将输出附加到它.

要使用 TextAreaOutputStream 类,[yo]你应该使用:

用法

//创建 javax.swing.JTextArea 控件的实例JTextArea txtConsole = new JTextArea();//现在创建一个新的 TextAreaOutputStream 来写入我们的 JTextArea 控件并包装一个//围绕它的 PrintStream 以支持 println/printf 方法.PrintStream out = new PrintStream(new TextAreaOutputStream(txtConsole));//将标准输出流重定向到 TextAreaOutputStreamSystem.setOut(out);//将标准错误流重定向到 TextAreaOutputStreamSystem.setErr(out);//现在测试机制System.out.println("Hello World");

I have posted two pieces of code below. Both codes work fine individually. Now, when I run the file Easy, and click on the "Start" button, I want the class AddNumber to be implemented. I mean to say that, instead of the AddNumber running on the console, is there any way I could make AddNumber run in the JTextArea i have created in the first class upon clicking the "Start" button? I thought maybe by action listener?(the way we do in case of buttons) But I'm not sure. Is there any other way to make my JTextArea act as a console for the other .java files?

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

The second class:

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

I have seen a few posts talking about PrintStream..but i don't think that applies here. Please help me out. Thanks :)

UPDATE: well i found this link: http://www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1 and it works in the sense that it shows "Enter two numbers to be added "...but where can the user provide his input?

EDIT: I just had to make a reference of the console in the main method of my class...and it works... well, not exactly as i would've wished to..but partly..the input still has to go from the terminal of the IDE..

解决方案

If you do a Google search for: "stdout JTextArea", you will a couple of links to solve your problem.

  • http://www.coderanch.com/t/458147/GUI/java/Redirect-output-stderr-stdout-JTextArea
  • Redirecting System.out to JTextPane
  • http://www.jcreator.com/forums/index.php?showtopic=773

In the last link, buddybob extends java.io.OutputStream to print standard output to his JTextArea. I included his solution below.

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:

Usage

// 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" );

这篇关于JTextArea 作为控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:创建一个“命令"安慰 下一篇:Java 中 System.out.println() 的字符限制

相关文章

最新文章