如何让我的JPasswordField使用特定密码?

时间:2015-02-26 15:04:58

标签: java swing passwords jpasswordfield

Hello Guys我需要一个我想在JPasswordField中编写的密码 (JPasswordFieldAnon)并将提交操作附加到两个JButton (AnonButton和AnonButton1)。

以下是代码:

package javafx;

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;

public class Anonymous {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Anonymous window = new Anonymous();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Anonymous() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setTitle("Anonymous Terminal. Enter Password");
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setSize(952, 785);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel JLabelAnon = new JLabel("We are Anonymous. We are Legion. We do not forgive. We do not forget. Expect Us.");
    JLabelAnon.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    JLabelAnon.setBackground(Color.BLACK);
    JLabelAnon.setFont(new Font("hooge 05_53", Font.PLAIN, 13));
    JLabelAnon.setForeground(Color.GREEN);
    JLabelAnon.setBounds(116, 566, 695, 14);
    frame.getContentPane().add(JLabelAnon);

    JPasswordField JPasswordFieldAnon = new JPasswordField("Enter Password: ", 1);
    JPasswordFieldAnon.setBackground(new Color(0, 128, 0));
    JPasswordFieldAnon.setToolTipText("Enter Password\r\n");
    JPasswordFieldAnon.setText("");
    JPasswordFieldAnon.setName("Anonymous Password");
    JPasswordFieldAnon.setBounds(367, 535, 206, 22);
    frame.getContentPane().add(JPasswordFieldAnon);

    JButton AnonButton = new JButton("Expect Us!");
    AnonButton.setBackground(new Color(34, 139, 34));
    AnonButton.setName("Log In");
    AnonButton.setBounds(579, 535, 115, 22);
    frame.getContentPane().add(AnonButton);

    JButton AnonButton1 = new JButton("Expect Us!");
    AnonButton1.setName("Log In");
    AnonButton1.setBackground(new Color(34, 139, 34));
    AnonButton1.setBounds(246, 535, 115, 22);
    frame.getContentPane().add(AnonButton1);

    JTextPane textPane1 = new JTextPane();
    textPane1.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    textPane1.setEditable(false);
    textPane1.setBackground(Color.BLACK);
    textPane1.setBounds(108, 563, 703, 20);
    frame.getContentPane().add(textPane1);

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon("F:\\FBI-Terminal_1.png"));
    lblNewLabel.setBounds(0, 0, 948, 759);
    frame.getContentPane().add(lblNewLabel);

}


    public void actionPerformed(ActionEvent e) {
    }
}

我的问题是我尝试了大约5天的时间,只是尝试了很多方法......

1 个答案:

答案 0 :(得分:2)

关于您的代码的一些注释。

  1. 您的密码字段变量的范围不足以在以后检索键入的密码,因为它是initialize()方法中的局部变量。我将它定义为类变量。

  2. 您必须在按钮上附加ActionListener才能实际“提交”密码。

  3. 您有一个actionPerformed()方法,但类标题中缺少ActionListener接口的实现。这就是我们在实现或覆盖现有方法时应该使用@Override注释的原因。

  4. 请参阅以下教程:


    题外话

    Swing旨在与Layout Managers一起使用。并且强烈建议不要使用setLocation(...)setBounds(...)setXxxSize(...)等方法。