2014-11-20 1 views
0

JButton이 JPasswordField에 각각의 숫자를 표시하도록합니다 (암호 점만 볼 수 있습니다). 나는 왜 그것이 그것을 지금 표시하지 않는지조차 모르겠다. 나는 무엇을 놓치고 있는가? 여기에 프로그램 실행을 보여 http://gyazo.com/b3fec4f8e433b05274cb0af78777ece6JPassword 필드에 JButton 표시 텍스트 만들기

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

여기 사용해야은 수정 된 코드

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

    } 
} 
입니다 (짧은 GIF를, 필요 다운로드 없습니다)
관련 문제