JPasswordField焦点问题

发布于 2024-12-03 14:01:58 字数 3553 浏览 1 评论 0原文

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

它有三个组件,

  1. JTextField 用于获取用户名
  2. JPasswordField 用于获取密码
  3. JButton 用于登录,

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

这是我为登录面板编写的代码,该代码可以编译并运行,但无法输入密码。有什么我错过的吗?

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

I wrote a very simple logon JPanel following the demo in http://download.oracle.com/javase/tutorial/uiswing/components/passwordfield.html

it has three components,

  1. JTextField to get username
  2. JPasswordField to get password
  3. JButton to logon

then the JPanel is shown in the JFrame when the application starts. the issue is, I found if I click on password field first, i can enter password without problem. however if I enter username first then I can't enter anything in the password field. anyone knows what might be going wrong here?

here is the the code I wrote for the logon panel, this code can be compiled and run, but the password can't be entered. is there something I missed?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-12-10 14:01:58

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

There is no problem as described in the question. This works fine. Once you enter the username and password, and click logon, the given username and password is printed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文