将信息从您的应用程序的一部分传递到另一个应用程序将取决于您的程序的结构。
在基本层面上,我建议将第一个屏幕中的值包装到某种自定义对象中。让我们用户User
。
User
将存储accountName
和password
作为private
实例字段的属性,可以通过 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
方法。对象不应该有 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
类以支持这一点。