package untitled1;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */public class Frame1 extends JFrame
{
  private JPanel contentPane;
  private XYLayout xYLayout1 = new XYLayout();
  //my controls
  private final int myLength = 5;
  private final int mySize = 50;
  private JLabel[][] myLabels = new JLabel[myLength][myLength];  //Construct the frame
  public Frame1()
  {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception
  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(258, 278));
    this.setTitle("Frame Title");
    this.MyControlsInIt();
    contentPane.addMouseListener(new java.awt.event.MouseAdapter()
    {
      public void mouseClicked(MouseEvent e)
      {
        contentPane_mouseClicked(e);
      }
    });
  }
  private void MyControlsInIt()
  {
    for (int i = 0;   i < this.myLength;   i++)
    {
      for (int j = 0;   j < this.myLength;   j++)
      {
        this.myLabels[i][j] = new JLabel();
        this.myLabels[i][j].setBorder(BorderFactory.createRaisedBevelBorder());
        contentPane.add(this.myLabels[i][j],
                        new XYConstraints (i*mySize, j*mySize, mySize, mySize));      }
    }
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e)
  {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING)
    {
      System.exit(0);
    }
  }  void contentPane_mouseClicked(MouseEvent e)
  {
    int i  = e.getX()/this.mySize;
    int j = e.getY()/this.mySize;
    this.myLabels[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
    this.myLabels[i][j].setForeground(Color.red);
    this.myLabels[i][j].setText("clicked");
  }
}

解决方案 »

  1.   

    ///:~开始啦,看好啊!
    package untitled1;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;/**
     * <p>Title:一个小例子 </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author sky000
     */public class Frame1 extends JFrame
    {
      private JPanel contentPane;
      private XYLayout xYLayout1 = new XYLayout();
      //my controls 小心点
      private final int myLength = 5;
      private final int mySize = 50;
      private JLabel[][] myLabels = new JLabel[myLength][myLength];  //Construct the frame
      public Frame1()
      {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      //小心异常啊
        try
        {
          jbInit();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception
      {
        setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(xYLayout1);
        this.setSize(new Dimension(258, 278));
        this.setTitle("Frame Title");
        this.MyControlsInIt();
        contentPane.addMouseListener(new java.awt.event.MouseAdapter()
        {
          public void mouseClicked(MouseEvent e)
          {
            contentPane_mouseClicked(e);
          }
        });
      }
      private void MyControlsInIt()
      {
        for (int i = 0;   i < this.myLength;   i++)
        {
          for (int j = 0;   j < this.myLength;   j++)
          {
            this.myLabels[i][j] = new JLabel();
            this.myLabels[i][j].setBorder(BorderFactory.createRaisedBevelBorder());
            contentPane.add(this.myLabels[i][j],
                            new XYConstraints (i*mySize, j*mySize, mySize, mySize));      }
        }
      }
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e)
      {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING)
        {
          System.exit(0);
        }
      }  void contentPane_mouseClicked(MouseEvent e)
      {
        int i  = e.getX()/this.mySize;
        int j = e.getY()/this.mySize;
        this.myLabels[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
        this.myLabels[i][j].setForeground(Color.red);
        this.myLabels[i][j].setText("clicked");
      }
    }
    ///:~
    不好意思,我只是把楼上的有些地方稍微改了一下。