0

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);
    }
}
Ahrdie
  • 19
  • 3
  • 3
    share your code what you did so far. – Satish Pahuja Oct 01 '17 at 04:40
  • Create a text file. Write the accepted values in Reg.java to that text file. Next, launch Get.java and in it read the text file you created in Reg.java – progyammer Oct 01 '17 at 04:42
  • Ok thanks, but How? – Ahrdie Oct 01 '17 at 05:12
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) In this case, the username/password would be better designed in a `JPanel` & displayed using a modal `JDialog` or a `JOptionPane`. If the frame of the application creates the panel (there's no need to extend `JPanel` here), it can reference the details of username and password as soon as the dialog / option pane is dismissed (the code statement immediately after it has been mad visible). – Andrew Thompson Oct 01 '17 at 07:14

3 Answers3

1

For a more general situation you might provide methods for accessing the user name and password, and a reference from the main class to the login class in order to call those methods.

But this does not require two Java classes, let alone two Java files. It also does not require using two frames, in fact, this is one case where multiple frames makes the task more tricky, in that a frame is non-modal. Use a modal JDialog or a JOptionPane instead of the 'login frame'. That way, the password can be checked as soon it is dismissed.

This is what it might look like: (tip: the valid password for all users is 'guest')

enter image description here

Here's how to go about it:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LoginRequired {

    private JTextField usernameField = new JTextField("Joe Blogs");
    private JPasswordField passwordField = new JPasswordField();
    char[] password = {'g', 'u', 'e', 's', 't'};
    JPanel loginPanel;

    LoginRequired() {
        JFrame f = new JFrame("Login Required");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JLabel output = new JLabel(
                "Login is required to use this application!",
                SwingConstants.CENTER);
        output.setBorder(new EmptyBorder(50, 100, 50, 100));
        f.add(output);
        f.pack();
        f.setResizable(false);
        f.setLocationByPlatform(true);
        f.setVisible(true);

        boolean loginValid = false;
        while (!loginValid) {
            showLogin(f);
            loginValid = isLoginValid();
        }
        String user = usernameField.getText();
        output.setText("Welcome back, " + user + "!");
        f.setTitle("Logged In: " + user);
    }

    private boolean isLoginValid() {
        char[] passwordEntered = passwordField.getPassword();
        if (passwordEntered.length != password.length) {
            return false;
        } else {
            for (int ii = 0; ii < password.length; ii++) {
                if (password[ii] != passwordEntered[ii]) {
                    return false;
                }
            }
            return true;
        }
    }

    private void showLogin(JFrame frame) {
        if (loginPanel==null) {
            loginPanel = new JPanel(new BorderLayout(5, 5));

            JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
            labels.add(new JLabel("User Name", SwingConstants.RIGHT));
            labels.add(new JLabel("Password", SwingConstants.RIGHT));
            loginPanel.add(labels, BorderLayout.WEST);

            JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
            usernameField = new JTextField("Joe Blogs");
            controls.add(usernameField);
            passwordField = new JPasswordField();
            controls.add(passwordField);
            loginPanel.add(controls, BorderLayout.CENTER);
        }
        passwordField.setText("");

        JOptionPane.showMessageDialog(
                frame, loginPanel, "Log In", JOptionPane.QUESTION_MESSAGE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginRequired();
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0
public class Reg extends JFrame implements ActionListener {
    .....
    '''''
    Get get;

    public Reg(){

        .....
        .....
        get = new Get();
    }
    @Override
    public void actionPerformed(ActionEvent e){
        String name = tf1.getText();
        String age = tf2.getText();
        get.print(name);
        get.print(age);
    }
 }

class Get{

    public void print(String txt) {
        System.out.println(txt);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
-2

Use BuferedReader with get.java and use BufferedWriter with reg.java. Use BufferedWriter in reg.java to write the Name and age in two lines and then use BufferedReader to read these values in Get.java.