如何在 if/if else 语句中使用 ActionListener 计算多个 JButton 的点击次数

How to count clicks for several JButtons with ActionListener in if/if else statements

我正在使用 java.swing 为带有 GUI 的投票系统制作一个项目。投票系统包含 5 个候选人,当您单击他们姓名旁边的投票按钮时,它应该递增并显示候选人目前拥有的票数。 我不知道为什么我的代码没有注册和计算每个按钮的投票,我已经尝试通过它但我被卡住了。

代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class VotingSystem implements ActionListener{
    
    private static JLabel label;
    private static JLabel candidate1;
    private static JLabel candidate2;
    private static JLabel candidate3;
    private static JLabel candidate4;
    private static JLabel candidate5;
    private static JButton button1;
    private static JButton button2;
    private static JButton button3;
    private static JButton button4;
    private static JButton button5;
    private static int counter1;
    private static int counter2;
    private static int counter3;
    private static int counter4;
    private static int counter5;


    
        public static void main(String[] args) {

            new VotingSystem();
        }
    
        //Creating the voting system method
        public VotingSystem() {
        
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
        
            frame.setSize(750, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
        
            panel.setLayout(null);
        
            candidate1 = new JLabel("Samir");
            candidate1.setBounds(10, 20, 80, 25);
            panel.add(candidate1);
            
            candidate2 = new JLabel("Christina");
            candidate2.setBounds(10, 60, 80, 25);
            panel.add(candidate2);
            
            candidate3 = new JLabel("Philip");
            candidate3.setBounds(10, 100, 80, 25);
            panel.add(candidate3);
            
            candidate4 = new JLabel("Gilgamesh");
            candidate4.setBounds(10, 140, 80, 25);
            panel.add(candidate4);
            
            candidate5 = new JLabel("Gonzales");
            candidate5.setBounds(10, 180, 80, 25);
            panel.add(candidate5);
        
            button1 = new JButton("Vote");
            button1.setBounds(100, 25, 60, 15);
            button1.addActionListener(new VotingSystem());
            panel.add(button1);
            
            button2 = new JButton("Vote");
            button2.setBounds(100, 65, 60, 15);
            button2.addActionListener(new VotingSystem());
            panel.add(button2);
            
            button3 = new JButton("Vote");
            button3.setBounds(100, 105, 60, 15);
            button3.addActionListener(new VotingSystem());
            panel.add(button3);
            
            button4 = new JButton("Vote");
            button4.setBounds(100, 145, 60, 15);
            button4.addActionListener(new VotingSystem());
            panel.add(button4);
            
            button5 = new JButton("Vote");
            button5.setBounds(100, 185, 60, 15);
            button5.addActionListener(new VotingSystem());
            panel.add(button5);
            
            
            label = new JLabel("Number of votes");
            panel.add(label);
            frame.setVisible(true);
                
        
        }


        @Override
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == button1) {
                
                label.setText("Number of votes: " + counter1++);
            }
            
            else if (e.getSource() == button2) {
    
                label.setText("Number of votes: " + counter2++);
            }
            
            else if (e.getSource() == button3) {
                
                label.setText("Number of votes: " + counter3++);
            }
            
            else if (e.getSource() == button4) {
        
                label.setText("Number of votes: " + counter4++);
            }
            
            else if (e.getSource() == button5) {
                
                label.setText("Number of votes: " + counter5++);
    
            }
        }
}
button1.addActionListener(new VotingSystem());
button2.addActionListener(new VotingSystem());
...

您创建了 5 个 VotingSystem 实例 class,这意味着您还有 5 个框架。

Don't keep creating instances of the VotingSystem class.

这意味着您需要完全重构您的代码。例如你不应该:

  1. 正在使用静态变量
  2. 使用空布局和 setBounds()。 Swing 旨在与布局管理器一起使用。

阅读 How to Use Buttons 上的 Swing 教程部分,了解一些基本示例以帮助您入门。

那就是下载演示,看看它们是如何工作的。然后修改工作代码以使用上面的基本逻辑。