我有一个要求,我需要调用类似的方法
SlotButtons.poiLabel1.setText(txtname.getText().toString())
但这里的 poiLabel1 会动态修改。可以修改为poiLabel2或poiLabel3或poiLabel4。数字是动态的。
谁能告诉我如何才能成功。
最佳答案
您可以使用数组。
假设您有一个带有按钮的数组:
JLabel[]labels=new JLabel[]{
SlotButtons.poiLabel0,
SlotButtons.poiLabel1,
SlotButtons.poiLabel2,
SlotButtons.poiLabel3,
SlotButtons.poiLabel4,
};
如果知道索引,就可以使用正确的标签:
labels[yourIndex].setText(txtname.getText().toString())
关于java - 使用动态引用调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490325/