语法错误就出现在分号那里,别的错误,你没有代码,我不清楚。我只好给你写了个示范
效果图

代码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();
}
}

这篇博客展示了如何在Java Swing中创建和管理多个按钮,包括为按钮添加事件监听器,处理按钮点击事件,并根据不同的按钮显示相应的消息。代码示例详细解释了如何实现按钮点击后的不同响应。
1060

被折叠的 条评论
为什么被折叠?



