我想我必须先解释我要做什么我创建了一个带有文本字段的 From 用于接受名称和另一个用于密码。输入必须存储在 UID 中的用户名和密码中的两个字符串中。
我们如何将值存储在字符串中,直到程序结束然后才打印它?无论main()
我将打印语句放在哪里,甚至在表单打开之前都会打印空值。
这是我的代码,由 Netbeans 生成。检查主要方法。
package mypackage;
public class test extends javax.swing.JFrame {
public test() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
username = new javax.swing.JTextField();
password = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("LOGIN");
username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usernameActionPerformed(evt);
}
});
password.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(107, 107, 107)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
.addComponent(password))
.addContainerGap(110, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(117, 117, 117)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(111, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernameActionPerformed
uid=username.getText();
}//GEN-LAST:event_usernameActionPerformed
private void passwordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordActionPerformed
pass=password.getText();
}//GEN-LAST:event_passwordActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test().setVisible(true);
}
});
System.out.println(uid+" "+pass);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField password;
private javax.swing.JTextField username;
// End of variables declaration//GEN-END:variables
static String uid,pass;
}