[英]How to pass JTextField from JFrame into another JFrame
当我在JFrame1
的JTextField
中输入名字,姓氏和其他信息,然后单击下一步时,输入的数据将在最后一个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 ...即使我输入用户名和密码的值也是如此。
将信息从应用程序的一部分传递到另一个应用程序将取决于程序的结构。
在基本级别上,我建议将第一个屏幕中的值包装到某种自定义对象中。 让我们为User
。
User
将accountName
和password
的属性存储为private
实例字段,可以通过getter对其进行访问。
基本上,您将在第一个屏幕上拥有某种吸气剂,该吸气剂将生成User
对象并将其传递回调用者。
第二个屏幕要么将User
对象作为构造函数的参数,要么作为setter。
大概,然后您可以将User
对象从编辑器窗格传递到JButton
的actionPerformed
方法内的视图窗格中
例如...
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;
}
}
因此,当您从NewAccountPane
调用getUser
时,将根据表单上字段的值构造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());
}
}
}
更新其他
传递值是编程的基本原则。
在您的代码中,您可以看到两种选择。
在您的StudentRegistrationForm_1
类的jButton_NextActionPerformed
中,您当前正在执行此操作...
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
对象。
在StudentRegistrationForm_1
类的jButton_NextActionPerformed
中,可以将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
类以支持此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.