因此,我是Java的新手,正在学习使用Swing,并且我目前正在从事由讲师分配给我的项目。这是某种蛮力软件的愚蠢版本,试图猜测用户键入的单词。
我不想打印所有猜出的单词,而是希望将它们显示在UI上的JLabel
中。 (这会不断向不同组合发送垃圾邮件。)
但是问题是:它仅在我的程序完成运行(因此找到了单词)后才显示一个猜测的单词。
我已经四处搜寻并寻找一种更新方法,但是我似乎找不到实际使用testField.setVisible(true);
的地方。
程序代码:
package gui;
import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Int;
import java.util.Random;
import java.util.Arrays;
import java.util.*;
public class bruteforcePanel extends javax.swing.JPanel {
String[] Alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
//String[] Alphabet = {"a"};
boolean startGuessing = false;
String pass;
String[] word;
String[] javaStuff;
List<String> guessedWord = new ArrayList<String>();
int amountOfGuesses;
String labelGuess;
private void startBruteforce(java.awt.event.ActionEvent evt) {
pass = textBoxPassword.getText();
//this.testField.setText(pass);
word = pass.split("");
//System.out.println(Arrays.toString(word));
startGuessing = true;
while(startGuessing == true) {
for (String i : word) {
Random r=new Random();
int randomNumber=r. nextInt(Alphabet. length);
String chosenLetter = Alphabet[randomNumber];
guessedWord.add(chosenLetter);
}
System.out.println(guessedWord);
labelGuess = String.join("", guessedWord);
this.testField.setText(labelGuess);
if (guessedWord.equals(Arrays.asList(word))) {
startGuessing = false;
System.out.println(amountOfGuesses);
} else {
guessedWord.clear();
amountOfGuesses += 1;
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton bruteforceButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel testField;
private javax.swing.JTextField textBoxPassword;
// End of variables declaration
}
来自JPanel
的代码:(位于private void startBruteforce(java.awt.event.ActionEvent evt) {}
之前)
public bruteforcePanel() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
textBoxPassword = new javax.swing.JTextField();
bruteforceButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
testField = new javax.swing.JLabel();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 146, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 205, Short.MAX_VALUE)
);
bruteforceButton.setText("Bruteforce");
bruteforceButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startBruteforce(evt);
}
});
jLabel1.setText("Word:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(bruteforceButton))
.addGroup(layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(testField)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(95, Short.MAX_VALUE)
.addComponent(textBoxPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(90, 90, 90))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(textBoxPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(bruteforceButton)
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(testField))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
}// </editor-fold>
所以就像我说的,我希望我的结果在标签中“垃圾邮件”。
答案 0 :(得分:2)
JLabel
,而是使用JTextArea
firstWordLowerCaseVariable
firstWordLowerCaseMethod()
FirstWordUpperCaseClass
ALL_WORDS_UPPER_CASE_CONSTANT
您将需要在JPanel#revalidate()
的{{1}}内部调用JPanel#repaint()
和ActionListener
方法(按此顺序),您可能需要将Timer
也包裹在JTextArea
内。
您需要使用JScrollPane
而不是Swing Timer
或任何其他类型的循环,因为它们会阻塞EDT,从而阻止其更新直到循环完成。