import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class WordGame extends javax.swing.JFrame implements ActionListener{
int SkipNum = 0;
String Get;
int counter2;
int score;
static String words[] = new String[20];
JButton Button[] = new JButton[5];
JFrame panel = new JFrame();
public WordGame() {
initComponents();
setWord();
}
public void clear(){
jButton1.setText("");
jButton2.setText("");
jButton3.setText("");
jButton4.setText("");
jButton5.setText("");
jButton6.setText("");
jButton7.setText("");
jButton8.setText("");
}
public void setWord() {
int index = (int)(Math.random()*20);
StringBuffer NS = new StringBuffer(words[index]);
String sh = "";
Get = words[index];
int counter = NS.length();
while (NS.length() != 0) {
int j = (int)(Math.random() * NS.length());
sh += NS.charAt(j);
NS.deleteCharAt(j);
}
if (counter == 5){
for (int i = 0; i < Button.length; i++){
for (int x = 0; x < 5;x++){
String Letter = (String.valueOf(sh.charAt(x)));//can't display the letter
Button[i] = new JButton(Letter);
Button[i].setText(Letter);
panel.add(Button[i]);
}
}
jButton6.setVisible(false);
jButton7.setVisible(false);
jButton8.setVisible(false);
}
else if (counter == 6){
jButton1.setText(String.valueOf(sh.charAt(0)));
jButton2.setText(String.valueOf(sh.charAt(1)));
jButton3.setText(String.valueOf(sh.charAt(2)));
jButton4.setText(String.valueOf(sh.charAt(3)));
jButton5.setText(String.valueOf(sh.charAt(4)));
jButton6.setText(String.valueOf(sh.charAt(5)));
jButton7.setVisible(false);
jButton8.setVisible(false);
}
else if (counter == 7){
jButton1.setText(String.valueOf(sh.charAt(0)));
jButton2.setText(String.valueOf(sh.charAt(1)));
jButton3.setText(String.valueOf(sh.charAt(2)));
jButton4.setText(String.valueOf(sh.charAt(3)));
jButton5.setText(String.valueOf(sh.charAt(4)));
jButton6.setText(String.valueOf(sh.charAt(5)));
jButton7.setText(String.valueOf(sh.charAt(6)));
jButton8.setVisible(false);
}
else if (counter == 8){
jButton1.setText(String.valueOf(sh.charAt(0)));
jButton2.setText(String.valueOf(sh.charAt(1)));
jButton3.setText(String.valueOf(sh.charAt(2)));
jButton4.setText(String.valueOf(sh.charAt(3)));
jButton5.setText(String.valueOf(sh.charAt(4)));
jButton6.setText(String.valueOf(sh.charAt(5)));
jButton7.setText(String.valueOf(sh.charAt(6)));
jButton8.setText(String.valueOf(sh.charAt(7)));
}
jButton1.setEnabled(true);
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
jButton7.setEnabled(true);
jButton8.setEnabled(true);
jTextField1.setText("");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + jButton1.getText());
jButton1.setEnabled(false);
}
BufferedReader dataIn = new BufferedReader(new FileReader("words.txt"));
int ctr = 0;
words[ctr] = dataIn.readLine();
while (ctr < words.length-1){
ctr++;
words[ctr] = dataIn.readLine();
}
我正在使用BufferedReader
阅读一些字符,我想在JButton
上显示每个字符。例如,我在文本文件上有一个5个字母的单词,我希望在随机格式的JButton
“NOTES”上显示每个单词。但我无法显示单词或文字。