Java JTextArea 作为控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19834155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTextArea as console
提问by nicki
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?
我在下面发布了两段代码。两个代码单独工作正常。现在,当我运行 Easy 文件并单击“开始”按钮时,我希望实现类 AddNumber。我的意思是说,不是在控制台上运行 AddNumber,有什么方法可以让 AddNumber 在我单击“开始”按钮后在第一类中创建的 JTextArea 中运行?我想也许是通过动作监听器?(我们在按钮的情况下所做的方式)但我不确定。有没有其他方法可以让我的 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);
}
}
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 :)
我看过一些关于 PrintStream 的帖子……但我认为这不适用于这里。请帮帮我。谢谢 :)
UPDATE: well i found this link: http://www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1and it works in the sense that it shows "Enter two numbers to be added "...but where can the user provide his input?
更新:我找到了这个链接:http: //www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1并且它的工作原理是它显示“输入两个要添加的数字” ...但是用户可以在哪里提供他的输入?
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..
编辑:我只需要在我的班级的主要方法中引用控制台......它可以工作......好吧,不完全是我希望的......但部分......输入仍然必须从IDE的终端去..
回答by Mr. Polywhirl
If you do a Google search for: "stdout JTextArea", you will a couple of links to solve your problem.
如果您在 Google 上搜索:“stdout JTextArea”,您将获得几个链接来解决您的问题。
- 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
- 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
In the last link, buddybobextends java.io.OutputStream
to print standard output to his JTextArea. I included his solution below.
在最后一个链接中,buddybob扩展java.io.OutputStream
以将标准输出打印到他的 JTextArea。我在下面包含了他的解决方案。
TextAreaOutputStream.java
文本区域输出流.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 thejava.io.OutputStream
class and overrides itswrite(int)
method overload, this class uses a reference to ajavax.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:
在
TextAreaOutputStream
扩展了java.io.OutputStream
类和覆盖其write(int)
方法的过载,这类使用的参考javax.swing.JTextArea
控制实例,然后输出附加到它时,它的写(INT B)方法被调用。要使用这个
TextAreaOutputStream
类,[你]你应该使用:
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" );