我的问题是如何在buttonHandler()中使用循环来确定按下的按钮是否具有大于之前的值。所以我正在寻找的方法是确保点击按钮的任何人根据他们看到的数字按升序点击它们。这是一个基于按升序点击按钮来评估灵活性的GUI游戏。
这是按钮阵列填充1-12的数字的方法,这也确保没有重复的数字,所以它将包含1-12的所有数字。
public static JButton[] randNum ()
{
arrayImage [0] = (int) (Math.random () * 12) + 1;
arrayImage [1] = (int) (Math.random () * 12) + 1;
while (arrayImage [1] == arrayImage [0])
{
arrayImage [1] = (int) (Math.random () * 12) + 1;
}
arrayImage [2] = (int) (Math.random () * 12) + 1;
while (arrayImage [2] == arrayImage [1] || arrayImage [2] == arrayImage [0])
{
arrayImage [2] = (int) (Math.random () * 12) + 1;
}
arrayImage [3] = (int) (Math.random () * 12) + 1;
while (arrayImage [3] == arrayImage [2] || arrayImage [3] == arrayImage [1] || arrayImage [3] == arrayImage [0])
{
arrayImage [3] = (int) (Math.random () * 12) + 1;
}
arrayImage [4] = (int) (Math.random () * 12) + 1;
while (arrayImage [4] == arrayImage [3] || arrayImage [4] == arrayImage [2] || arrayImage [4] == arrayImage [1] || arrayImage [4] == arrayImage [0])
{
arrayImage [4] = (int) (Math.random () * 12) + 1;
}
arrayImage [5] = (int) (Math.random () * 12) + 1;
while (arrayImage [5] == arrayImage [4] || arrayImage [5] == arrayImage [3] || arrayImage [5] == arrayImage [2] || arrayImage [5] == arrayImage [1] || arrayImage [5] == arrayImage [0])
{
arrayImage [5] = (int) (Math.random () * 12) + 1;
}
arrayImage [6] = (int) (Math.random () * 12) + 1;
while (arrayImage [6] == arrayImage [5] || arrayImage [6] == arrayImage [4] || arrayImage [6] == arrayImage [3] || arrayImage [6] == arrayImage [2] || arrayImage [6] == arrayImage [1] || arrayImage [6] == arrayImage [0])
{
arrayImage [6] = (int) (Math.random () * 12) + 1;
}
arrayImage [7] = (int) (Math.random () * 12) + 1;
while (arrayImage [7] == arrayImage [6] || arrayImage [7] == arrayImage [5] || arrayImage [7] == arrayImage [4] || arrayImage [7] == arrayImage [3] || arrayImage [7] == arrayImage [2] || arrayImage [7] == arrayImage [1] || arrayImage [7] == arrayImage [0])
{
arrayImage [7] = (int) (Math.random () * 12) + 1;
}
arrayImage [8] = (int) (Math.random () * 12) + 1;
while (arrayImage [8] == arrayImage [7] || arrayImage [8] == arrayImage [6] || arrayImage [8] == arrayImage [5] || arrayImage [8] == arrayImage [4] || arrayImage [8] == arrayImage [3] || arrayImage [8] == arrayImage [2] || arrayImage [8] == arrayImage [1] || arrayImage [8] == arrayImage [0])
{
arrayImage [8] = (int) (Math.random () * 12) + 1;
}
arrayImage [9] = (int) (Math.random () * 12) + 1;
while (arrayImage [9] == arrayImage [8] || arrayImage [9] == arrayImage [7] || arrayImage [9] == arrayImage [6] || arrayImage [9] == arrayImage [5] || arrayImage [9] == arrayImage [4] || arrayImage [9] == arrayImage [3] || arrayImage [9] == arrayImage [2] || arrayImage [9] == arrayImage [1] || arrayImage [9] == arrayImage [0])
{
arrayImage [9] = (int) (Math.random () * 12) + 1;
}
arrayImage [10] = (int) (Math.random () * 12) + 1;
while (arrayImage [10] == arrayImage [9] || arrayImage [10] == arrayImage [8] || arrayImage [10] == arrayImage [7] || arrayImage [10] == arrayImage [6] || arrayImage [10] == arrayImage [5] || arrayImage [10] == arrayImage [4] || arrayImage [10] == arrayImage [3] || arrayImage [10] == arrayImage [2] || arrayImage [10] == arrayImage [1] || arrayImage [10] == arrayImage [0])
{
arrayImage [10] = (int) (Math.random () * 12) + 1;
}
arrayImage [11] = (int) (Math.random () * 12) + 1;
while (arrayImage [11] == arrayImage [10] || arrayImage [11] == arrayImage [9] || arrayImage [11] == arrayImage [8] || arrayImage [11] == arrayImage [7] || arrayImage [11] == arrayImage [6] || arrayImage [11] == arrayImage [5] || arrayImage [11] == arrayImage [4] || arrayImage [11] == arrayImage [3] || arrayImage [11] == arrayImage [2] || arrayImage [11] == arrayImage [1] || arrayImage [11] == arrayImage [0])
{
arrayImage [11] = (int) (Math.random () * 12) + 1;
}
for (int x = 0 ; x <= 11 ; x++)
{
buttonArray [x] = new JButton ("" + arrayImage [x]);
buttonArray [x].setBackground (Color.orange);
}
for (int i = 0 ; i <= 11 ; i++)
{
buttonArray [i].setActionCommand ("" + arrayImage [i]);
}
return buttonArray;
}
这是我在main方法中引入按钮数组的方式。
static JButton btnStart, buttonArray[] = new JButton [12];
给它们颜色,并且循环用于按顺序给它们每个动作监听器。
for (int x = 0 ; x <= 11 ; x++)
{
buttonArray [x] = new JButton ("");
buttonArray [x].setBackground (Color.orange);
}
ButtonHandler onClick = new ButtonHandler ();
btnStart.addActionListener (onClick);
for (int y = 0 ; y <= 11 ; y++)
{
buttonArray [y].addActionListener (onClick);
}
这里是面板的介绍,并将面板添加到面板中。然后将面板添加到框架中..
JPanel gameBoard = new JPanel (new GridLayout (2, 6));
gameBoard.setBackground (Color.orange);
JPanel gameContent = new JPanel (new GridLayout (1, 2));
JPanel mainGame = new JPanel (new BorderLayout ());
//GAme Board
gameContent.add (btnStart);
gameContent.add (scoreOutput);
gameBoard.add (buttonArray [0]);
gameBoard.add (buttonArray [1]);
gameBoard.add (buttonArray [2]);
gameBoard.add (buttonArray [3]);
gameBoard.add (buttonArray [4]);
gameBoard.add (buttonArray [5]);
gameBoard.add (buttonArray [6]);
gameBoard.add (buttonArray [7]);
gameBoard.add (buttonArray [8]);
gameBoard.add (buttonArray [9]);
gameBoard.add (buttonArray [10]);
gameBoard.add (buttonArray [11]);
mainGame.add (gameBoard, BorderLayout.CENTER);
mainGame.add (gameContent, BorderLayout.PAGE_END);
Container contentPane1 = questions.getContentPane ();
contentPane1.add (mainGame);
questions.setSize (500, 250);
questions.setVisible (true);
答案 0 :(得分:0)
首先,您必须将数字作为按钮的名称。 你可以这样做: 取一个数组和一个变量:
int pressed[] = new int[12];
int n;
在构造函数中将n的值设置为0。 现在在actionPerformed(ActionEvent ae)方法:
if(pressed.length==0)
pressed[n]=Integer.parseInt(ae.getActionCommand());
else
Check if last number in array is less than current number
else show error
您可以使用以下方式获取最后一个号码 压[pressed.length-1]