如何使密码字段等于密码123456和按钮的密码;“好的”;在java中使用它

如何使密码字段等于密码123456和按钮的密码;“好的”;在java中使用它,java,jbutton,jpasswordfield,Java,Jbutton,Jpasswordfield,我想当我点击“编辑、添加和更新程序”按钮时,密码字段出现,按钮“确定”出现。ok按钮应该调用displaytable()方法,该方法将显示一个可编辑的表供用户更改。 我如何获得密码才能工作 JButton passcode = new JButton("Edit, add or update programme");//creates button to display editable table passcode.setBounds(50,200,95,30);

我想当我点击“编辑、添加和更新程序”按钮时,密码字段出现,按钮“确定”出现。ok按钮应该调用displaytable()方法,该方法将显示一个可编辑的表供用户更改。 我如何获得密码才能工作

JButton passcode = new JButton("Edit, add or update programme");//creates button to display editable table
    passcode.setBounds(50,200,95,30);
        passcode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) { //here
                Char[] password = "123456";
                JButton button = new JButton("OK");
                JPasswordField pf = new JPasswordField();
                button.addActionListener(e1 -> {

                    if(JPasswordField.getPassword.equals(password){
                        panel1.add(pf);
                        panel1.add(button); //TO HERE IM HAVING A PROBLEM WITH
                    }
                });
            }
        });

更好的设计是最初添加这些组件,但禁用它们(setEnabled(false))。无论如何,要回答场景中的问题,您应该在UI线程上运行代码,并在添加新组件后重新验证和重新绘制

                if(JPasswordField.getPassword().equals(password){
                   SwingUtilities.invokeLater(new Runnable() {
                      public void run() {
                        panel1.add(pf);
                        panel1.add(button); //TO HERE IM HAVING A PROBLEM WITH
                        panel1.validate();
                        panel1.repaint();
                     }
                  });
                }

请不要对我们大喊大叫。在互联网上,所有的大写字母都相当于叫喊。谢谢你,我不是故意的,我很抱歉,请描述一下你到底遇到了什么问题。您的意思是要将密码控制的初始值设置为123456,但您不知道如何操作?@Avas如果这解决了您的问题,请选中答案旁边的复选标记