如何将字符串从一个java文件传递到另一个java文件

时间:2017-10-01 04:36:32

标签: java swing

我创建两个Java文件,一个是Reg.java,第二个是Get.java。在Reg.java中,我创建了一个JFrame,其中包含用于年龄的名称和文本字段的文本字段,以及一个按钮。我想要的是当你在文本字段中输入名称和年龄并单击按钮时,它将传递字符串名称和年龄并显示在Get.java中。

这是我的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);
    }
}

3 个答案:

答案 0 :(得分:1)

对于更一般的情况,您可以提供访问用户名和密码的方法,以及从主类到登录类的引用,以便调用这些方法。

但这不需要两个Java类,更不用说两个Java文件了。它也不需要使用两个帧,事实上,这是一种情况,其中多个帧使任务更棘手,因为帧是非模态的。使用模式JDialogJOptionPane代替“登录框架”。这样,一旦解密,就可以检查密码。

这可能是这样的:(提示:所有用户的有效密码是'来宾')

enter image description here

以下是如何解决这个问题:

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();
            }
        });
    }
}

答案 1 :(得分: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);
    }
}

答案 2 :(得分:-2)

将BuferedReader与get.java一起使用,并将BufferedWriter与reg.java一起使用。 在reg.java中使用BufferedWriter将Name和age写成两行,然后使用BufferedReader在Get.java中读取这些值。