java判断哪个按钮被点击
publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstub//判断是那个按钮被点击if(...
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//判断是那个按钮被点击
if(e.getActionCommand().equals("aa"));
{
System.out.println("你点击黑色按钮");
}else if(e.getActionCommand().equals("bb"))
{
System.out.println("你点击了红色按钮");
}else {
System.out.println("不知道");
}
}
错误:Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Syntax error on token "else", delete this token 展开
// TODO Auto-generated method stub
//判断是那个按钮被点击
if(e.getActionCommand().equals("aa"));
{
System.out.println("你点击黑色按钮");
}else if(e.getActionCommand().equals("bb"))
{
System.out.println("你点击了红色按钮");
}else {
System.out.println("不知道");
}
}
错误:Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Syntax error on token "else", delete this token 展开
1个回答
展开全部
if(e.getActionCommand().equals("aa"));
这句代码结尾多了个分号,删除后就应该可以了
if(e.getActionCommand().equals("aa"))
追问
不是那个,它错误显示在else if 的else上面,删除是对的,但显示的结果不是我所要的
追答
语法错误就出现在分号那里,别的错误,你没有代码,我不清楚。我只好给你写了个示范
效果图
代码
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonDemo extends JFrame implements ActionListener {
JButton jb1, jb2,jb3,jb4;
public ButtonDemo() {
jb1 = new JButton("aa");
jb1.addActionListener(this);
jb2 = new JButton("bb");
jb2.addActionListener(this);
add(jb1);add(jb2);
jb3= new JButton("cc");
jb3.addActionListener(this);
jb4 = new JButton("dd");
jb4.addActionListener(this);
add(jb3);add(jb4);
setLayout(new FlowLayout());
setSize(260, 160);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// 判断是那个按钮被点击
if (e.getActionCommand().equals("aa")) {
System.out.println("你点击黑色按钮");
} else if (e.getActionCommand().equals("bb")) {
System.out.println("你点击了红色按钮");
} else {
System.out.println("不知道");
}
}
public static void main(String[] args) {
new ButtonDemo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询