import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UsedPane extends JFrame implements MouseListener{
int toolFlag = 1;
int count = 0;
boolean isMove = false;
JToolBar tools = new JToolBar("工具");
JButton tool;
JButton addTool = new JButton("add");
public UsedPane(){
setLayout(new BorderLayout());
tools.add(addTool);
add(tools,BorderLayout.CENTER);
addTool.addMouseListener(this);
addTool.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent et){
tool = new JButton(String.valueOf(count));
isMove = true;
tools.add(tool);
count++;
}
});
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){
if(isMove){
tool.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent et){
toolFlag = count-1;
System.out.println(toolFlag);
}});
isMove = false;
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public static void main(String[] args){
UsedPane a = new UsedPane();
a.setSize(400,100);
a.setVisible(true);
}
}
我想让添进去的按钮点时输出按钮上的值,但为什么都为最后一个按钮的值呢????
清高手指点一下~~~~~