我有问题。我想创建一个类型为
的数组private JButton[] butte;
butte[3] = button;
解雇此类代码以失败告终
所有代码
public void LoadAllConfigureToCard(JPanel map)
{
Component[] component = map.getComponents();
for(int i=0; i<component.length; i++)
{
if (component[i] instanceof JButton)
{
final JButton button = (JButton)component[i];
button.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent arg0) {
if(nocarddrag)
{
Rectangle butt = button.getBounds();
int w = butt.width;
int h = butt.height;
PointerInfo a = MouseInfo.getPointerInfo();
Point pt = a.getLocation();
Rectangle movc = new Rectangle(pt.x, pt.y, w, h);
button.setBounds(movc);
cardisdrag = true;
butte[3] = button;
}
}
});
}
}
}
如何才能在int类型的数组中给出前缀?
答案 0 :(得分:3)
您需要先初始化数组:
private JButton[] butte = new JButton[100]; // or any other size you may want
答案 1 :(得分:2)
尝试
JButton[] btnArray = new JButton[10];
btnArray[3] = someButton;
你忘了创建一个对象。