简体   繁体   中英

How to change the JTextField values inside a loop

I need some help with my program. I'm trying to make a brute force program as a project and I don't know how to make the loop values appear in the JTextField in the print () method. I don't just need the final value of the loop. I know this question has been asked a lot and I've tried everything but it's just not working. Please can anyone help me? I'll appreciate it !

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

  }
}

For example, the string 123 has length 3 so the permutations can be 000,001,002 etc

Then why only display one at t time?

Instead I would use a JTextArea and append each value to the text field so all values are visible at the same time. Nobody is going to remember all the values if you display one at a time.

Otherwise, you would use a Swing Timer to display one at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM