java 获取下标

来源:百度知道 编辑:UC知道 时间:2024/06/04 02:16:21
import java.awt.*;
import java.awt.event.*;
public class usfo1 extends WindowAdapter implements WindowListener,ActionListener
{
Frame f;
Button[] but;
public usfo1()
{f=new Frame();
f.setLayout(null);
f.setSize(1024,768);
f.addWindowListener(this);
f.setBackground(new Color(0,0,0));
int k=0;
but=new Button[90];
for (int i=0;i<=8;i++)
{
for(int j=0;j<=9;j++)
{
but[k]=new Button(""+k);
but[k].setSize(50,50);
but[k].setLocation(j*50+10,i*50+35);
but[k].addActionListener(this);
f.add(but[k]);

k++;
}
}

f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//就是想获取but的下标
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main (String[] args)
{
usfo1 k=new

public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());//这样就输出了按钮的的值
比如你点1 就会输出1 }
}

package com.ajax.test;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;

public class Usfo1 extends WindowAdapter implements WindowListener,
ActionListener {
Frame f;
Button[] but;

public Usfo1() {
f = new Frame();
f.setLayout(null);
f.setSize(1024, 768);
f.addWindowListener(this);
f.setBackground(new Color(0, 0, 0));
int k = 0;
but = new Button[90];
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 9; j++) {
but[k] = new Button("" + k);
but[k].setSize(50, 50);
but[k].setLocation(j * 50 + 10, i * 50 + 35);
but[k].addActionListener(this);
f.add(but[k]);

k++;
}
}
f.setVisible(true);