我需要有关如何使用代码actionlistener的帮助或指导,以便我可以循环jButtons。 它乘以十六,因为它是在txt文件中读取的文件中的单词数。
import java.awt.Container;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class wordGame extends JFrame implements ActionListener {
}
int index = (int)(Math.random()*16);
StringBuffer s = new StringBuffer(words[index]);
String sh = "";
bb = words[index];
int counter = s.length();
Container pane = getContentPane();
JFrame panel = new JFrame();
JButton b[] = new JButton [16];
int i;
while (s.length() != 0) {
int j = (int)(Math.random() * s.length());
sh = sh + s.charAt(j);
s.deleteCharAt(j);
}
if (counter == 5){
for (i = 0; i < 5; i++) {
b = new JButton[i];
panel.add(b[i]);
}
pane.add(panel);
for (int j = 0; j< 5; j++) {
b[i].setText(String.valueOf(sh.charAt(j)));
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)));
jButton6.setVisible(false);
jButton7.setVisible(false);
jButton8.setVisible(false);
}
}
答案 0 :(得分:0)
你的意思是你想要遍历它们吗?
如果是这种情况,我可能会使用多维数组(或arraylist)..
JButton[][] bttns = new JButton[5][16]();
for(int i = 0; i < bttns.length; i++)
{
for(int i2 = 0; i2 < bttns[0].length; i2++)
{
bttns[i][i2].setText( String.valueOf(sh.charAt(i*bttns[0].length + i2)) );
bttns[i][i2].setVisible(True);;
}
}
等