I'm a newbie to Java and I want to pass some outputs to outside of the current class in Java. First, I asked user to put some numbers and stored as string. Then converted those into integers. What I want to do is turning those values into some methods. Here is my code.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
JTextField x = new JTextField("X",5);
JTextField y = new JTextField("Y",5);
JTextField z = new JTextField("Z",5);
JButton chk = new JButton("Check");
public Main(){
JPanel newPanel = new JPanel();
newPanel.add(x);
newPanel.add(y);
newPanel.add(z);
newPanel.add(chk);
chk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//These are the values I want to pass
int xi = Integer.parseInt(x.getText());
int yi = Integer.parseInt(y.getText());
int zi = Integer.parseInt(z.getText());
System.out.printf("you data is %d\n",xi);
System.out.printf("Your data is %d\n",yi);
System.out.printf("you data is %d",zi);
}
});
add(newPanel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[]args) {
new Main().setVisible(true);
}
}
As you see, I want to pass those outputs (namely xi, yi and zi) into another class. Like following...
int result = xi+yi+zi;
System.out.println(result);
How can I do that? How to pass those data into another class? Thanks