Java 如何将 JTextField 从 JFrame 传递到另一个 JFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18296398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass JTextField from JFrame into another JFrame
提问by JeraldPunx
When I input First Name, Last Name and other information in the JTextField
of JFrame1
and then click button next, the inputted data will display all inputted data on last JFrame
.
Example: this is animated gif, the last frame is what I want, cause I still don't know how to make it:
当我在JTextField
of 中输入 First Name、Last Name 等信息JFrame1
然后单击 next 按钮时,输入的数据将显示 last 上的所有输入数据JFrame
。
示例:这是动画 gif,最后一帧是我想要的,因为我仍然不知道如何制作:
How can I do this? I'm sorry if this is a newbie question but I'm learning..
我怎样才能做到这一点?如果这是一个新手问题,我很抱歉,但我正在学习..
I am using NetBeans GUI builder.
我正在使用 NetBeans GUI 构建器。
EDIT: I created like this idk if i'm doing it right...
编辑:如果我做对了,我会像这个 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;
}
}
and in my DisplayFrame is i don't know what put inside
在我的 DisplayFrame 中,我不知道里面放了什么
public void setUser(User user) {
// idk what to put here... maybe the jLabel? please help
}
NEW UPDATES ON QUESTION
问题的新更新
the button on my StudentRegistrationForm_1.java
我的按钮 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();*/
}
and the class
和班级
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;
}
while the StudentRegistrationForm_3
I added a constructor and method like this
而StudentRegistrationForm_3
我添加了这样的构造函数和方法
StudentRegistrationForm_3(User user) {
name.setText(user.getName());
}
public void setUser(User user) {
name.setText(user.getName());
}
idk why still gave me null... even I input the value on username and password..
idk 为什么仍然给我空...即使我输入了用户名和密码的值..
采纳答案by MadProgrammer
Passing information from part of your application to another will depend on the structure of your program.
将信息从应用程序的一部分传递到另一个将取决于程序的结构。
At the basic level, I would recommend wrapping the values from the first screen into some kind of custom object. Let's user User
.
在基本级别,我建议将第一个屏幕中的值包装到某种自定义对象中。让我们用户User
.
User
would store the properties of accountName
and password
as private
instance fields, which would be accessible via getters.
User
将accountName
和的属性存储password
为private
实例字段,这些字段可以通过 getter 访问。
Basically, you would have some kind of getter on the first screen that would generate the User
object and pass it back to the caller.
基本上,您会在第一个屏幕上使用某种 getter 来生成User
对象并将其传递回调用者。
The second screen would either take a User
object as a parameter to the constructor or as setter.
第二个屏幕要么将User
对象作为构造函数的参数,要么作为 setter。
Presumbably, you would then pass the User
object from your editor pane to your view pane within the actionPerformed
method of your JButton
大概,然后您会将User
对象从编辑器窗格传递到您的actionPerformed
方法中的视图窗格JButton
For example...
例如...
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
*/
}
}
And in your actionPerformed
method...
而在你的actionPerformed
方法...
public void actionPerformed(ActionEvent evt) {
User user = instanceOfNewAccountPane.getUser();
instanceOfAccountDetailPane.setUser(user);
// Switch to instanceOfAccountDetailPane
}
Updated from updates to question
从更新更新到问题
Your user object is almost right, but I would get rid of the getUser
method. The User
object should have no concept of the UI not should it need to interact with it directly...
您的用户对象几乎是正确的,但我会摆脱该getUser
方法。该User
对象应该没有UI的概念,它不应该需要直接进行交互与它...
So instead of...
所以,而不是...
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;
}
}
I'd be tempered to do something like...
我会很乐意做类似的事情...
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;
}
}
So when you called getUser
from your NewAccountPane
, you would construct the User
object based on the values of the fields on the form.
因此,当您getUser
从您的调用时NewAccountPane
,您将User
根据表单上字段的值构造对象。
Basic working example
基本工作示例
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());
}
}
}
Update with additional
用额外的更新
Passing values is a fundamental principle to programming.
传递值是编程的基本原则。
In your code, you have two choices to that I can see..
在您的代码中,您有两个选择,我可以看到..
In the jButton_NextActionPerformed
of your StudentRegistrationForm_1
class, you are currently doing this...
在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();
}
But there is no way for StudentRegistrationForm_3
to access the User
object you have created.
但是无法StudentRegistrationForm_3
访问User
您创建的对象。
In the jButton_NextActionPerformed
of your StudentRegistrationForm_1
class, you can either pass the User
object to the constructor of the instance of StudentRegistrationForm_3
that you create
在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();
}
Or modify StudentRegistrationForm_3
to have a method that accepts a User
object
或者修改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();
}
Either way, you will need to modify the StudentRegistrationForm_3
class to support this.
无论哪种方式,您都需要修改StudentRegistrationForm_3
该类以支持这一点。