首先定義一擱MyButton文件如下﹕
import javax.swing.*;
public class MyButton extends JButton
{
public int i;
public int j;
public boolean flag;
public MyButton(String str)
{
super(str);
}
public void setRow( int i)
{
this.i=i;
}
public void setCol( int j)
{
this.j=j;
}
public void setFlag(boolean flag)
{
this.flag=flag;
}
public int getRow()
{
return i;
}
public int getCol()
{
return j;
}
public boolean getFlag()
{
return flag;
}
}
----------------------------------
然后MyGame文件如下﹕
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGame extends JFrame
{
Container cp;
MyButton buttons[][]=new MyButton[4][6];//為何在這里沒有錯 下面卻出錯
public MyGame()
{
super("我的游戲");
setSize(400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel mainPanel=new JPanel();
JPanel subPanel=new JPanel();
mainPanel.setLayout(new GridLayout(4,6));
initButtons();
for(int i=0;i<buttons.length;i++)
for(int j=0;j<buttons[i].length;j++)
mainPanel.add(buttons[i][j]);
cp.add(subPanel,BorderLayout.NORTH);
cp.add(mainPanel,BorderLayout.CENTER);
}
public void initButtons()
{
for(int i=0;i<buttons.length;i++)
for(int j=0;j<buttons[i].length;j++)
{
buttons[i][j]=new MyButton();//這里出錯
buttons[i][j].i=i;
buttons[i][j].j=j;
buttons[i][j].flag=false;
}
}
public static void main(String[] agrs)
{
MyGame win=new MyGame();
win.setVisible(true);
}
}
-------------------
出錯信息﹕
F:\game\MyGame.java:36: cannot resolve symbol
symbol : constructor MyButton ()
為何在這里出錯呢﹖謝謝﹗~~