nic*_*cki 6 java console swing console-application jtextarea
我在下面发布了两段代码.两个代码都可以单独使用.现在,当我运行文件Easy,然后单击"开始"按钮时,我希望实现类AddNumber.我的意思是说,而不是在控制台上运行的AddNumber,有没有什么方法可以让我在单击"开始"按钮后在第一个类中创建的JTextArea中运行AddNumber?我想也许是动作听众?(我们按钮的方式)但我不确定.有没有其他方法可以让我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
第二课:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我看过几篇关于PrintStream的帖子.但我认为这不适用于此.请帮帮我.谢谢 :)
更新:我发现这个链接:http://www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1 它的工作原理是它显示"输入两个要添加的数字" ...但是用户可以在哪里提供他的输入?
编辑:我只需要在我班级的主要方法中引用控制台...它的工作原理......好吧,不完全像我希望的那样......但是部分......输入仍然需要从IDE的终端开始..
如果你在谷歌搜索:"stdout JTextArea",你会有几个链接来解决你的问题.
在最后一个链接中,buddybob扩展java.io.OutputStream
为将标准输出打印到他的JTextArea.我在下面提到他的解决方案
/*
*
* @(#) 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 ) );
}
}
Run Code Online (Sandbox Code Playgroud)
在
TextAreaOutputStream
扩展了java.io.OutputStream
类和覆盖其write(int)
方法的过载,这类使用的参考javax.swing.JTextArea
控制实例,然后输出附加到它时,它的写(INT B)方法被调用.要使用
TextAreaOutputStream
该类,[你好]你应该使用:
// 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" );
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18047 次 |
最近记录: |