如何更改循环内的JTextField值

时间:2016-04-20 12:59:11

标签: java swing

我的程序需要一些帮助。我试图将一个暴力程序作为一个项目,我不知道如何使循环值出现在print()方法的JTextField中。我不需要循环的最终值。我知道这个问题已经被问了很多,我已经尝试了所有的东西,但它只是不起作用。请有人帮帮我吗?我很感激!

public class BF  implements ActionListener {
private JPanel contentPane;
private JTextField password;
private JTextField length;
public JTextField generateField;
JRadioButton numbers;
JButton generate;
JLabel passwordfound;
JLabel timelabel;
int min;
int max; 
int stringLength; 
private  int[] chars;
String n="";
char[] passCom;
JFrame frame;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                BF frame = new BF();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public BF() {
     frame = new JFrame ("Brute Force Attack Program");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100, 100, 754, 665);

    frame.setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.setLocationRelativeTo(null);
    frame.setContentPane(contentPane);
    contentPane.setLayout(null);
    try {
        UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch(Exception e) {   
    }
    backgroundPanel panel = new backgroundPanel();
    panel.setBounds(0, 0, 749, 633);
    contentPane.add(panel);
    panel.setLayout(null);

    generate = new JButton("Find Password");
    generate.setFont(new Font("Tahoma", Font.BOLD, 15));
    generate.setBounds(292, 401, 145, 35);
    generate.addActionListener(this);
    panel.add(generate);

    generateField = new JTextField();
    generateField.setForeground(Color.WHITE);
    generateField.setFont(new Font("Tahoma", Font.BOLD, 24));
    generateField.setEditable(false);
    generateField.setBounds(315, 449, 186, 54);
    generateField.setBackground(new Color(0,0,0,0));
    generateField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
    panel.add(generateField);
    generateField.setColumns(10);


    frame.setVisible(true);


}
public void run(char min, char max, int len)

{
    this.min = min;

    this.max = max;

    this.stringLength = len;

    chars = new int[stringLength + 1];

    Arrays.fill(chars, 1, chars.length, min);


  while (chars[0] == 0&&!n.equals(password.getText()))

  {


 print();

 increment();
 n=String.valueOf(passCom);

  }


}

public void increment()

{

  for (int i = chars.length - 1; i >= 0; i--)

  {

   if (chars[i] < max)

   {

    chars[i]++; return;

    }

  chars[i] = min;

  }

}

public void print()

{
   int x=0;
   for (int i = 1; i < chars.length; i++)

  {
    passCom[x]=(char)chars[i];
     x++;
    String txt = new String (passCom);
    generateField.setText(txt);


  }

}




@Override
public void actionPerformed(ActionEvent e) {

    if (!password.getText().equals("") && e.getSource() == generate && !length.getText().equals("")&&numbers.isSelected()){
        int count = Integer.parseInt(length.getText());
        passCom = new char [count];
            run('0','9',count);
  }

  }
}

1 个答案:

答案 0 :(得分:1)

  

例如,字符串123的长度为3,因此排列可以是000,001,002等。

然后为什么只在t时间显示一个?

相反,我会在文本字段中使用JTextArea和append每个值,以便所有值同时可见。如果您一次显示一个值,则没有人会记住所有值。

否则,您将使用Swing Timer一次显示一个。