0

JTextField当我在of中输入 First Name、Last Name 等信息,JFrame1 然后单击 next 按钮时,输入的数据将在 last 上显示所有输入的数据JFrame
示例:这是动画 gif,最后一帧是我想要的,因为我仍然不知道如何制作它:

在此处输入图像描述

我怎样才能做到这一点?如果这是一个新手问题,我很抱歉,但我正在学习..

我正在使用 NetBeans GUI 构建器。

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

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

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

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 我添加了一个这样的构造函数和方法

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

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

4

1 回答 1

4

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

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

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

基本上,您会在第一个屏幕上有某种吸气剂,它会生成User对象并将其传回给调用者。

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

大概,然后您将在您的方法中将User对象从编辑器窗格传递到视图窗格actionPerformedJButton

例如...

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方法。对象不应该有 UI的User概念,也不应该直接与它交互......

所以,而不是...

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类以支持这一点。

于 2013-08-18T06:47:32.643 回答