I create Two Java File, the one is Reg.java and the second is Get.java. in Reg.java I create a JFrame with textfield for name and textfield for age, and a button. all i want is when you enter the name and age in textfields and click button, it will pass the string name and age and show in Get.java.
this is my code for Reg.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Reg extends JFrame implements ActionListener {
private Container con = getContentPane();
FlowLayout fl = new FlowLayout();
JLabel lb1 = new JLabel(": ");
JTextField tf1 = new JTextField(14);
JLabel lb2 = new JLabel("Enter your Age: ");
JTextField tf2 = new JTextField(14);
JButton btnSub = new JButton("Submit");
public Reg(){
setLayout(fl);
setSize(350, 275);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(btnSub);
lb1.setAlignmentX(LEFT_ALIGNMENT);
lb2.setAlignmentX(LEFT_ALIGNMENT);
lb1.setPreferredSize(new Dimension(120,50));
lb2.setPreferredSize(new Dimension(120,50));
tf1.setAlignmentX(RIGHT_ALIGNMENT);
tf2.setAlignmentX(RIGHT_ALIGNMENT);
btnSub.setHorizontalAlignment(JButton.CENTER);
btnSub.setToolTipText("Click to Submit");
btnSub.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String name = tf1.getText();
String age = tf2.getText();
}
public static void main(String[] args){
Reg fr = new Reg();
fr.setVisible(true);
}
}