使JButton在JPasswordField中显示文本

时间:2014-11-20 03:42:40

标签: java swing text jbutton jpasswordfield

我希望JButtons在JPasswordField上显示各自的数字(我知道我只会看到密码点)。我甚至不确定为什么现在不显示它,我错过了什么?这里我展示了运行的程序:(简短的gif,无需下载) http://gyazo.com/b3fec4f8e433b05274cb0af78777ece6

package tarea10;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Tarea10 {
private JFrame f;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
private JPasswordField passField;
private JPanel p;

public Tarea10() {
    p = new JPanel();
    passField = new JPasswordField("");
    f = new JFrame("Programa");
    b1 = new JButton("7");
    b2 = new JButton("8");
    b3 = new JButton("9");
    b4 = new JButton("4");
    b5 = new JButton("5");
    b6 = new JButton("6");
    b7 = new JButton("1");
    b8 = new JButton("2");
    b9 = new JButton("3");
    b10 = new JButton("Cancelar");
    b11 = new JButton("0");
    b12 = new JButton("Aceptar");      
}

    ActionListener al = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
    JButton button = (JButton) e.getSource();
    String prev = passField.getText();
    passField.setText(prev + button.getText());
    b1.addActionListener(al);
    b2.addActionListener(al);
    b3.addActionListener(al);
    b4.addActionListener(al);
    b5.addActionListener(al);
    b6.addActionListener(al);
    b7.addActionListener(al);
    b8.addActionListener(al);
    b9.addActionListener(al);
    b11.addActionListener(al);  
}
};

public void mostrar() {
    p.setLayout (new GridLayout(4,3));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(b10);
    p.add(b11);
    p.add(b12);
    p.add(passField);
    f.add(passField, BorderLayout.WEST);
    f.add(p, BorderLayout.EAST);
    f.setSize(450,400);
    f.setVisible(true);
}

public static void main(String args[]) {

  Tarea10 ventana = new Tarea10();
  ventana.mostrar();

}}

1 个答案:

答案 0 :(得分:1)

你应该在这里使用更正后的代码

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Tarea10 {
    private JFrame f;
    private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
    private JPasswordField passField;
    private JPanel p;

    public Tarea10() {
        p = new JPanel();
        passField = new JPasswordField("");
        f = new JFrame("Programa");
        b1 = new JButton("7");
        b2 = new JButton("8");
        b3 = new JButton("9");
        b4 = new JButton("4");
        b5 = new JButton("5");
        b6 = new JButton("6");
        b7 = new JButton("1");
        b8 = new JButton("2");
        b9 = new JButton("3");
        b10 = new JButton("Cancelar");
        b11 = new JButton("0");
        b12 = new JButton("Aceptar");   
        ActionListener al = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            JButton button = (JButton) e.getSource();
            String prev = passField.getText();
            passField.setText(prev + button.getText());

        } 
      };
       b1.addActionListener(al);
       b2.addActionListener(al);
       b3.addActionListener(al);
       b4.addActionListener(al);
       b5.addActionListener(al);
       b6.addActionListener(al);
       b7.addActionListener(al);
       b8.addActionListener(al);
       b9.addActionListener(al);
       b11.addActionListener(al);  

    }


    public void mostrar() {
        p.setLayout (new GridLayout(4,3));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(b10);
    p.add(b11);
    p.add(b12);
    p.add(passField);
    f.add(passField, BorderLayout.WEST);
    f.add(p, BorderLayout.EAST);
    f.setSize(450,400);
    f.setVisible(true);
}

public static void main(String args[]) {

  Tarea10 ventana = new Tarea10();
  ventana.mostrar();

  }
}