我对Panel的输出有疑问。我想按钮而不是面板
我的java代码
for(int i = 0;i < 100;i++)
{
if(buttonG[i] == null)
buttonG[i] = new JButton();
}
int location = 5;
for(int i = 0;i < 10;i++)
{
buttonG[i].setText("Group-"+(i+1));
buttonG[i].setSize(80,50);
buttonG[i].setLocation(location,10);
buttonG[i].setVisible(true);
location = location + 95;
jPanel1.add(buttonG[i]);
buttonG[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
输出:
答案 0 :(得分:0)
BtnAction action = new BtnAction();
for(int i = 0;i < 10;i++) {
buttonG[i].addActionListener(action);
}
class BtnAction implement ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand==enter){
//enterButtonCode. i assume that you have a btn named enter
}
{
}
你正在做的事情并没有错,但没有意义,因为你无法在actionPerformed方法中添加任何内容。创建一个实现ActionListner的内部类,并使用addActionListner方法传递BtnAction.try的对象,在上面的方法中执行它。