我在下面发布了两段代码。两个代码单独工作正常。现在,当我运行文件 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);
}
}
第二类:
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 的帖子..但我认为这不适用于这里。请帮帮我。谢谢 :)
更新:好吧,我找到了这个链接: http: //www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1 它的工作原理是它显示“输入两个要添加的数字” ...但是用户可以在哪里提供他的输入?
编辑:我只需要在我的班级的主要方法中引用控制台......它工作......好吧,不完全像我希望的那样......但部分......输入仍然必须从IDE的终端去..