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);
}
}
我想让添进去的按钮点时输出按钮上的值,但为什么都为最后一个按钮的值呢????
清高手指点一下~~~~~
----------------解决方案--------------------------------------------------------
你的意思是什么呢? 我试了 就是按一次后面显示从0开始 0+1的 结果
----------------解决方案--------------------------------------------------------
不是,是添加的按钮显示自己按钮上的值~~~~~
----------------解决方案--------------------------------------------------------
例如我点了add按钮6次,添加了从0--5六个按钮,我按0按钮时输出0,1输出1,等等~~~~~
----------------解决方案--------------------------------------------------------
终于想到了,代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UsedPane extends JFrame implements MouseListener{
String toolFlag = "";
int count = 0;
boolean isPress = 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));
count++;
isPress = true;
tools.add(tool);
if(isPress){
tool.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent et){
toolFlag = et.getActionCommand();
System.out.println(toolFlag);
}});}
}
});
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){
isPress = 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);
}
}
----------------解决方案--------------------------------------------------------
你是要添加按纽 而不是数字 是吧?
----------------解决方案--------------------------------------------------------
对啊~~~~
原来的事件添加有问题,我想了两天,郁闷着呢??
还好今天想出来了,呵呵~~~~~~
----------------解决方案--------------------------------------------------------
能想出来最好 继续努力吧
----------------解决方案--------------------------------------------------------
呵呵~~~~~
谢谢~~~
----------------解决方案--------------------------------------------------------