问题
I'm new in Java, just want to ask a simple question I have my first form and a text field, I input a text there the once I click the button a new form will come out and the Text in the text Field will be come a Label the the new form
I try this code but it doesn't work
public class Data extends javax.swing.JFrame {
public Data() {
initComponents();
FrmLogIn f = new FrmLogIn();
User.setText(f.UName.getText());
User.setVisible(true);
}
回答1:
There are many ways you could tackle this problem. You could create your own JDialog to handle the input requirements (and I probably would in most cases), but if you're after something a little simpler, JOptionPane provides a ready made dialog which is highly configurable.


There's, of course, nothing stopping you from mixing the two concpets (customized JDialog and JOptionPane)
public class TestLogin01 {
public static void main(String[] args) {
new TestLogin01();
}
public TestLogin01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
LoginPane loginPane = new LoginPane();
String userName = null;
boolean validUser = false;
int result = JOptionPane.CANCEL_OPTION;
do {
result = JOptionPane.showConfirmDialog(null, loginPane, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (result) {
case JOptionPane.OK_OPTION:
// Verify user details..
userName = loginPane.getUserName();
char[] password = loginPane.getPassword();
// Simple random test...
validUser = ((int) (Math.round(Math.random() * 1))) == 0 ? true : false;
if (!validUser) {
JOptionPane.showMessageDialog(null, "Inavlid username/password", "Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
} while (!validUser && result != JOptionPane.CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
if (validUser) {
JOptionPane.showMessageDialog(null, "Welcome back valued user " + userName, "Welcome", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
}
public class LoginPane extends JPanel {
private JTextField userName;
private JPasswordField passwordField;
public LoginPane() {
userName = new JTextField(10);
passwordField = new JPasswordField(10);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("User name: "), gbc);
gbc.gridy++;
add(new JLabel("Password: "), gbc);
gbc.gridy = 0;
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(userName, gbc);
gbc.gridy++;
add(passwordField, gbc);
}
public String getUserName() {
return userName.getText();
}
public char[] getPassword() {
return passwordField.getPassword();
}
}
}
You might like to have a read through How to create GUIs with Swing and How to use dialogs for more information.
回答2:
seeing your code, it seems that you have not included event handling part(and you need to include that to get your work done)
as an brief introduction you need to do this in your code:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FrmLogIn f = new FrmLogIn();
User.setText(f.UName.getText());
f.setVisible(true);
}
}
Note: assuming User is an JLabel and UName an JTextField
and FrmLogIn has extended JFrame and you have set required fields like layout and size of the JFrame
for more info about event-handling look up here
Edit 2:
sample code snippet - you want something like this ( its just a rough one to give you an idea how to move ahead wtih)
edit 3
as commented by @madProgrammer - replaced null layout by FlowLayout
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.*;
import javax.swing.JTextField;
class FrmLogIn extends JFrame{
JLabel User;
public FrmLogIn() {
setLayout(new FlowLayout());
setSize(200,200);
User = new JLabel("");
// User.setBounds(20,30,100,40);
add(User);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class ForTest extends JFrame{
JButton enter;
JTextField UName;
public ForTest() {
setLayout(new FlowLayout());
setSize(300,300);
enter = new JButton("enter");
//enter.setBounds(20,20,100,30);
UName = new JTextField();
//UName.setBounds(40,80,60,30);
add(UName);
add(enter);
setVisible(true);
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FrmLogIn f = new FrmLogIn();
f.User.setText(UName.getText());
f.setVisible(true);
setVisible(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ForTest();
}
}
来源:https://stackoverflow.com/questions/14825442/java-text-field-of-form-to-new-form