Java JTextArea作为控制台
我在下面发布了两段代码。这两种代码单独工作都很好。现在,当我轻松地运行文件并单击“开始”按钮时,我希望实现类AddNumber。我的意思是,除了在控制台上运行AddNumber之外,还有什么方法可以让AddNumber在我在第一个类中创建的JTextArea中运行,只要单击“Start”按钮就可以了?我想也许是通过行动听者(就像我们在按钮上做的那样),但我不确定。有没有其他方法可以让我的JTextArea充当其他.java文件的控制台Java JTextArea作为控制台,java,swing,console,console-application,jtextarea,Java,Swing,Console,Console Application,Jtextarea,我在下面发布了两段代码。这两种代码单独工作都很好。现在,当我轻松地运行文件并单击“开始”按钮时,我希望实现类AddNumber。我的意思是,除了在控制台上运行AddNumber之外,还有什么方法可以让AddNumber在我在第一个类中创建的JTextArea中运行,只要单击“Start”按钮就可以了?我想也许是通过行动听者(就像我们在按钮上做的那样),但我不确定。有没有其他方法可以让我的JTextArea充当其他.java文件的控制台 import java.awt.*; import 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的帖子,但我认为这不适用于这里。
请帮帮我。谢谢:)
更新:我发现了这个链接:它的作用是显示“输入两个要添加的数字”…但是用户在哪里可以提供他的输入
编辑:我只需要在我的类的主方法中引用控制台…它可以工作。。。嗯,不完全如我所愿……但部分……输入仍然必须来自IDE的终端。如果你在谷歌上搜索:“stdout JTextArea”,你会找到几个链接来解决你的问题
java.io.OutputStream
,将标准输出打印到他的JTextArea。我在下面列出了他的解决方案
TextAreaOutputStream.java
TextAreaOutputStream
扩展了java.io.OutputStream
类
并重写其write(int)
方法重载,该类使用
引用javax.swing.JTextArea
控件实例,然后
每当调用其write(intb)方法时,都向其追加输出
要使用TextAreaOutputStream
类,您应该使用:
用法
嗨,谢谢你的回答。上面的代码是否意味着我应该创建另一个.java文件TextAreaOutputStream。然后将使用代码添加到我的AddNumber类的主方法中?因为我看不到有任何变化……或者我不是很了解它吗?好吧,正如用法所示,您需要传递
JTextArea
的引用,并将系统输出设置到AddNumber
类中,以便将系统调用发送到JTextArea
。我仍然无法让它工作:(很抱歉让人恼火,但是已经尝试了很长一段时间,但没有任何成功..这对我不起作用..应用代码时,我的控制台和文本区域都不会显示任何sysout。。。
/*
*
* @(#) 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 ) );
}
}
// 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" );