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..