I'm making a project for a voting-system with GUI using java.swing. The voting system contains of 5 candidates and when you click the vote button next to their name it should increment and display the number of votes that the candidate has at the moment. I dont know why my code is not registrating and counting the votes of each button, i have tried to work through it but i am stuck.
code:
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 );
}
}
}
CodePudding user response:
button1.addActionListener(new VotingSystem());
button2.addActionListener(new VotingSystem());
...
You create 5 instance of the VotingSystem class, which means you also have 5 frames.
Don't keep creating instances of the VotingSystem class
.
This means you need to completely restructure your code. For example you should not:
- be using static variable
- be using a null layout and setBounds(). Swing was designed to be used with layout managers.
Read the section from the Swing tutorial on How to Use Buttons for some basic examples to get you started.
That is download the demos to see how they work. Then modify the working code to use your basic logic from above.