import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
class Test
{
public static int num[] = new int[42];
public static int numl = 16;
public static int numno = 0;
public static int chose = 0;
public static Boolean first = false;
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JPanel jplPanel = new JPanel();
int i;
int j=1;
int num1 , num2;
//if(first == false){
for( i = 0 ;i<16;i++){
num[i] = i+1;
}
//first = true;
//}
JButton jbnButton[] = new JButton[16];
JButton changeButton[] = new JButton[2];
jplPanel.setLayout (new GridLayout(4, 4));
for(i = 0;i<=15;i++){
jbnButton[i]=new JButton("Button"+(i+1));
jplPanel.add(jbnButton[i]);
String s = Integer.toString(i+1);
jbnButton[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random ran = new Random();
chose = ran.nextInt(numl);
System.out.println(chose);
int i;
for( i=0; i<jbnButton.length; i++){
if(e.getSource() == jbnButton[i]){
jbnButton[i].setText(""+num[chose]);
}
}
for(i = chose ;i<numl;i++){
num[i] = num[i+1];
//System.out.println(num[chose]);
}
numl--;
System.out.println(numl);
for(i = 0 ;i<numl;i++){
//System.out.println(num[i]);
}
}
});
}
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("Yang");
}
}
沒有留言:
張貼留言