如何随机重命名按钮。JAVA
我为我的战舰游戏创建了一个10x10的网格。我所有的按钮都被命名为数字,我希望它们按顺序从A1到J10命名,但我不知道该如何命名如何随机重命名按钮。JAVA,java,Java,我为我的战舰游戏创建了一个10x10的网格。我所有的按钮都被命名为数字,我希望它们按顺序从A1到J10命名,但我不知道该如何命名 public class Center_Panel extends JPanel { public Center_Panel() { this.setLayout(new GridLayout(1, 2, 10, 10)); JPanel panel1 = new JPanel(new GridLayout(10, 10));
public class Center_Panel extends JPanel {
public Center_Panel() {
this.setLayout(new GridLayout(1, 2, 10, 10));
JPanel panel1 = new JPanel(new GridLayout(10, 10));
panel1.setBackground(Color.BLUE);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
JButton button = new JButton(Integer.toString(i + +j));
panel1.add(button);
button.addActionListener (new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e){
}});
//GridCellP1 [i][j] = button;
}
}
public class Center\u面板扩展了JPanel{
公共中心面板(){
这个.setLayout(新的GridLayout(1,2,10,10));
JPanel panel1=新的JPanel(新的网格布局(10,10));
镶板1.立根背景(颜色:蓝色);
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
jbuttonbutton=newjbutton(Integer.toString(i++j));
面板1.添加(按钮);
button.addActionListener(新建ActionListener())
{
@凌驾
已执行的公共无效操作(操作事件e){
}});
//GridCellP1[i][j]=按钮;
}
}
对于char数据类型,您可以使用++
运算符遍历:
public Center_Panel() {
this.setLayout(new GridLayout(1, 2, 10, 10));
JPanel panel1 = new JPanel(new GridLayout(10, 10));
panel1.setBackground(Color.BLUE);
char c = 'A';
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
JButton button = new JButton(c + "" + j);
panel1.add(button);
button.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
}});
//GridCellP1 [i][j] = button;
}
c++;
}
}
public Center_Panel(){
这个.setLayout(新的GridLayout(1,2,10,10));
JPanel panel1=新的JPanel(新的网格布局(10,10));
镶板1.立根背景(颜色:蓝色);
字符c='A';
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
JButton按钮=新JButton(c+“”+j);
面板1.添加(按钮);
button.addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
}});
//GridCellP1[i][j]=按钮;
}
C++;
}
}
使用char
键入值,添加或减去ASCII值以更改其值
比如说,
char c = 'a';
c++; // c now equals b
然而,如果你不能通过索引访问它们,你可以用两种不同的方法
方式1:
使用数组存储所有值
char[] array = {'a','b, 'c'...,'z'}
// now you can access by index
char c = array[3];
// c equals the value of d
方式2:
构造一个按字母表中的位置输出字符的方法
// starting number of the alphabet is 0 (because i'm a programmer.) :D
private char getChar(int value){
return value + 65;
}
此链接将帮助您理解ASCII表
你要的东西中的随机性在哪里?