如何将JTextField从JFrame传递到另一个JFrame

时间:2013-08-18 06:36:16

标签: java swing jframe jtextfield

当我在JTextField的{​​{1}}中输入名字,姓氏和其他信息时 然后点击下一步按钮,输入的数据将显示最后JFrame1的所有输入数据 示例:这是动画gif,最后一帧是我想要的,因为我仍然不知道如何制作它:

enter image description here

我该怎么做?如果这是一个新手问题,我很抱歉,但我正在学习..

我正在使用NetBeans GUI构建器。

编辑: 如果我做得对,我就像这个idk一样创造......

JFrame

在我的DisplayFrame中我不知道里面放了什么

public class User {
    private String username;
    private String password;
    public User() {
        username = null;
        password = null;
    }

    public User getUser() {
        User user = new User();
        username = TexUsername.getText();
        return user;
    }
}

问题的最新更新

public void setUser(User user) { // idk what to put here... maybe the jLabel? please help }

上的按钮
StudentRegistrationForm_1.java

和班级

private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {
        User user = new User(TexUsername.getText(),Password.getPassword());
        StudentRegistrationForm_3 form = new StudentRegistrationForm_3(user);
        form.setUser(user);
        this.dispose();
    } catch(Exception e){JOptionPane.showMessageDialog(null, e);}
    /*
    new StudentRegistrationForm_2().setVisible(true);
    this.dispose();*/

} 

public class User { private String name; private char[] password; public User(String name, char[] password) { this.name = name; this.password = password; } public String getName() { return name; } public char[] getPassword() { return password; } } public User getUser() { User user = new User(TexUsername.getText(), Password.getPassword()); return user; } 我添加了一个像这样的构造函数和方法

StudentRegistrationForm_3

idk为什么还给我null ...即使我输入用户名和密码的值..

1 个答案:

答案 0 :(得分:4)

将信息从应用程序的一部分传递到另一个应用程序取决于程序的结构。

在基本级别,我建议将第一个屏幕中的值包装到某种自定义对象中。我们是用户User

User会将accountNamepassword的属性存储为private个实例字段,这些字段可以通过getter访问。

基本上,你会在第一个屏幕上使用某种getter来生成User对象并将其传递给调用者。

第二个屏幕将User对象作为构造函数的参数或作为setter。

然后,您可以将User对象从编辑器窗格传递到actionPerformed

JButton方法中的视图窗格

例如......

public class NewAccountPane extends JPanel {
    /*...*/
    public User getUser() {
        User user = new User();
        /* Take the values from the fields and apply them to the User Object */
        return user;
    }
}

public class AccountDetailsPane extends JPanel {
    /*...*/
    public void setUser(User user) {
        /* Take the values from the User object
         * and apply them to the UI components
         */
    }
}

并使用actionPerformed方法......

public void actionPerformed(ActionEvent evt) {
    User user = instanceOfNewAccountPane.getUser();
    instanceOfAccountDetailPane.setUser(user);
    // Switch to instanceOfAccountDetailPane
}

更新了问题

您的用户对象几乎是正确的,但我会摆脱getUser方法。 User对象应该没有UI的概念,不应该直接与它进行交互...

所以不是......

public class User {
    private String username;
    private String password;
    public User() {
        username = null;
        password = null;
    }

    public User getUser() {
        User user = new User();
        username = TexUsername.getText();
        return user;
    }
}

我会做一些像......这样的事情。

public class User {
    private String username;
    private char[] password;
    public User(String username, char[] password) {
        this.username = username;
        this.password = password;
    }

    public String getUserName() {
        return username;
    }

    public char[] getPassword() {
        return password;
    }
}

因此,当您从getUser调用NewAccountPane时,您将根据表单上字段的值构建User对象。

基本工作示例

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Passon {

    public static void main(String[] args) {
        new Passon();
    }

    private JPanel basePane;
    private EditorPane editorPane;
    private DisplayPane displayPane;

    public Passon() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                basePane = new JPanel(new CardLayout());
                basePane.add((editorPane = new EditorPane()), "Editor");
                basePane.add((displayPane = new DisplayPane()), "Display");
                ((CardLayout)basePane.getLayout()).show(basePane, "Editor");
                frame.add(basePane);

                JPanel buttons = new JPanel();
                JButton next = new JButton("Next >");
                buttons.add(next);
                frame.add(buttons, BorderLayout.SOUTH);

                next.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        CardLayout layout = (CardLayout) basePane.getLayout();
                        displayPane.setUser(editorPane.getUser());
                        layout.show(basePane, "Display");
                        ((JButton)e.getSource()).setEnabled(false);
                    }
                });

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class User {

        private String name;
        private char[] password;

        public User(String name, char[] password) {
            this.name = name;
            this.password = password;
        }

        public String getName() {
            return name;
        }

        public char[] getPassword() {
            return password;
        }

    }

    public class EditorPane extends JPanel {

        private JTextField name;
        private JPasswordField password;

        public EditorPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(new JLabel("User: "), gbc);
            gbc.gridy++;
            add(new JLabel("Password: "), gbc);

            gbc.gridy = 0;
            gbc.gridx++;

            name = new JTextField(20);
            password = new JPasswordField(20);

            add(name, gbc);
            gbc.gridy++;
            add(password, gbc);        
        }

        public User getUser() {
            User user = new User(name.getText(), password.getPassword());
            return user;
        }

    }

    public class DisplayPane extends JPanel {

        private JLabel name;

        public DisplayPane() {
            name = new JLabel();
            setLayout(new GridBagLayout());
            add(name);
        }

        public void setUser(User user) {
            name.setText(user.getName());
        }            
    }
}

使用其他

进行更新

传递值是编程的基本原则。

在你的代码中,我有两个选择,我可以看到..

jButton_NextActionPerformed课程的StudentRegistrationForm_1中,您目前正在执行此操作...

private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
    new StudentRegistrationForm_3().setVisible(true);
    // How is StudentRegistrationForm_3 suppose to reference the User object??
    User user = new User(TexUsername.getText(),Password.getPassword());
    this.dispose();
}                                            

StudentRegistrationForm_3无法访问您创建的User对象。

jButton_NextActionPerformed类的StudentRegistrationForm_1中,您可以将User对象传递给您创建的StudentRegistrationForm_3实例的构造函数

private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
    User user = new User(TexUsername.getText(),Password.getPassword());
    new StudentRegistrationForm_3(user).setVisible(true);
    this.dispose();
}                                            

或修改StudentRegistrationForm_3以获得接受User对象

的方法
private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
    User user = new User(TexUsername.getText(),Password.getPassword());
    StudentRegistrationForm_3 form = new StudentRegistrationForm_3(user);
    form.setUser(user);
    this.dispose();
}                                            

无论哪种方式,您都需要修改StudentRegistrationForm_3类来支持此功能。