等待文本字段输入

时间:2013-07-06 06:34:03

标签: java string swing jtextfield

我想我必须先解释一下我要做的事情 我创建了一个From with text字段用于接受名称而另一个用于接受密码。 输入必须存储在UID中的两个字符串用户名和密码传递中。

如何在程序结束之前将值存储在字符串中,然后才打印它? 无论我在哪个main()放置print语句,都会在表单打开之前打印空值。

这是我的代码,由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;
}

3 个答案:

答案 0 :(得分:4)

使用模式JDialogJOptionPane进行此用途。

有关详细信息,请参阅How to Use Modality in Dialogs。关于如何使用它们阻止应用程序的进度。直到他们关闭。

其他提示

1)

username = new javax.swing.JTextField();

应该是:

// suggest a size in letters..
username = new javax.swing.JTextField(12);

2)

password = new javax.swing.JTextField();

应该是:

// use a field specialized for this purpose
password = new javax.swing.JPasswordField();

3)

不要延伸框架或其他顶层容器。而是创造&amp;使用一个实例。

答案 1 :(得分:1)

只有当字段具有焦点时按ActionListener,才会执行JTextField中的Enter

当然,您可以随时调用getText()来检索文本字段包含的内容。

如果您使用Tab来在输入字段之间切换,则不会触发侦听器。

如果您想确保在用户未按Enter键的情况下关闭窗口时获取密码,则可以添加WindowListener并在窗口关闭时读取值。或者您可以在文本字段中添加FocusListener,以便在用户离开时也会收到通知。

答案 2 :(得分:0)

您应该在框架中添加windowListener并覆盖windowClosing

这是有效的init的修改版本。

private void initComponents() {
    this.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                    uid=username.getText();
                    pass=password.getText();


                System.out.println(uid+" "+pass);

        }
    });

    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();
}