如何仅使用一个JButton对每次点击执行不同的操作.. 请帮我.. 这是我的代码..
btn1.addActionListener(new ActionListener(){
int clicks;
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
Object source = e.getSource();
if(source == btn1){
clicks++;
if(clicks==1){
txt1.setText("a");
clicks=0;
}
if(clicks==2){
txt1.setText("b");
clicks=0;
}
if(clicks==3){
txt1.setText("c");
clicks=0;
}
}
}
});
请帮帮我..
答案 0 :(得分:1)
您应该使用MouseListener/Adapter
而不是ActionListener
,实施mouseClicked
,并使用MouseEvent.getClickCount()
答案 1 :(得分:0)
您正在使用每个操作重置click
,因此只能采取行动" a"。
btn1.addActionListener(new ActionListener(){
int clicks;
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
Object source = e.getSource();
if(source == btn1){
if(clicks%3 == 0){
txt1.setText("a");
}
if(clicks%3 == 1){
txt1.setText("b");
}
if(clicks%3 ==2){
txt1.setText("c");
}
clicks++;
}
}
});