JTextArea作为控制台 [英] JTextArea as console
问题描述
我已经发布了两段代码。两个代码单独工作。现在,当我运行文件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){
//添加代码来调用其他类并使JTextArea充当控制台
}
});
}
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(输入要添加的两个数字);
Scanner in = new Scanner(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
ul>
在最后一个链接中, buddybob 扩展 java.io.OutputStream
他的JTextArea。我在下面添加了他的解决方案。
TextAreaOutputStream.java
/ *
*
* @(#)TextAreaOutputStream.java
*
* /
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/ **
*输出流,将其输出写入javax.swing.JTextArea
*控件。
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
* /
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;
/ **
*创建一个TextAreaOutputStream的新实例,它将
*写入到javax.swing.JTextArea控件的指定实例。
*
* @param control引用javax.swing.JTextArea
*控件,输出必须重定向到
*到。
* /
public TextAreaOutputStream(JTextArea control){
textControl = control;
}
/ **
*将指定的字节作为字符写入
* javax.swing.JTextArea。
*
* @param b要作为字符写入
* JTextArea的字节。
* /
public void write(int b)throws IOException {
//将数据作为字符追加到JTextArea控件
textControl.append(String.valueOf((char) b));
}
}
$ c> TextAreaOutputStream 扩展
java.io.OutputStream
类
并覆盖其write(int)
方法重载,此类使用
引用到javax.swing.JTextArea
控制实例,然后
将输出附加到它
要使用
TextAreaOutputStream
类,使用:
使用
//创建javax.swing.JTextArea控件的实例
JTextArea txtConsole = new JTextArea();
//现在创建一个新的TextAreaOutputStream来写入我们的JTextArea控件,并包装一个
// PrintStream,以支持println / printf方法。
PrintStream out = new PrintStream(new TextAreaOutputStream(txtConsole));
//将标准输出流重定向到TextAreaOutputStream
System.setOut(out);
//将标准错误流重定向到TextAreaOutputStream
System.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 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:
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作为控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!