首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JPasswordField焦点问题

JPasswordField焦点问题
EN

Stack Overflow用户
提问于 2011-09-07 20:16:33
回答 1查看 1.1K关注 0票数 0

我在JPanel的演示之后编写了一个非常简单的登录http://download.oracle.com/javase/tutorial/uiswing/components/passwordfield.html

它有三个组成部分,

username

  • JPasswordField让password

  • JButton登录

  1. JTextField

然后,当应用程序启动时,JPanel将显示在JFrame中。问题是,我发现如果我首先点击密码字段,我可以输入密码没有问题。但是,如果我先输入用户名,那么我就无法在密码字段中输入任何内容。有人知道这里可能出了什么问题吗?

这是我为登录面板编写的代码,这段代码可以编译和运行,但是密码不能输入。我错过了什么吗?

代码语言:javascript
复制
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class Logon extends javax.swing.JPanel implements ActionListener {

    private JTextField fldName;
    private JPasswordField fldPasswd;
    private JButton btnLogon;

    public Logon() {

        JLabel labTitle = new JLabel("title");
        labTitle.setText("EMT Monitor v.1.0");

        // username
        fldName = new JTextField(10);
        JLabel labName = new JLabel("Username");
        labName.setText("Username:");
        labName.setLabelFor(fldPasswd);

        // passwd
        fldPasswd = new JPasswordField(10);
        fldPasswd.setActionCommand("Logon");
        fldPasswd.addActionListener(this);
        fldPasswd.requestFocusInWindow();

        JLabel labPasswd = new JLabel("Password");
        labPasswd.setText("Password:");
        labPasswd.setLabelFor(fldPasswd);

        // botten
        btnLogon = new JButton("Logon");
        btnLogon.setActionCommand("Logon");
        btnLogon.addActionListener(this);


        JPanel mainPanel = new JPanel();


        btnLogon.setPreferredSize(new Dimension(200, 30));

        mainPanel.setPreferredSize(new Dimension(340, 190));

        mainPanel.add(labName);
        mainPanel.add(fldName);
        mainPanel.add(labPasswd);
        mainPanel.add(fldPasswd);
        mainPanel.add(btnLogon);

        JPanel outPanel = new JPanel();
        outPanel.setPreferredSize(new Dimension(400, 300));
        outPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

        outPanel.add(mainPanel);
        add(outPanel);

        setAlignmentX(Component.CENTER_ALIGNMENT);
        setAlignmentY(Component.CENTER_ALIGNMENT);
        setVisible(true);
    }



public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("Logon")) { //Process the password.

            String user = fldName.getText();
            String passwd = new String(fldPasswd.getPassword());

            System.out.println(user + " " + passwd);


        }


    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Logon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        final Logon newContentPane = new Logon();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        createAndShowGUI();
            }
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-12-08 17:05:36

问题中没有描述的问题。这个很好用。输入用户名和密码并单击“登录”后,将打印给定的用户名和密码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7340017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档