我正在使用 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 );
}
}
}
uj5u.com熱心網友回復:
button1.addActionListener(new VotingSystem());
button2.addActionListener(new VotingSystem());
...
您創建了 VotingSystem 類的 5 個實體,這意味著您還有 5 個框架。
Don't keep creating instances of the VotingSystem class.
這意味著您需要完全重構您的代碼。例如,您不應該:
- 使用靜態變數
- 使用空布局和 setBounds()。Swing 旨在與布局管理器一起使用。
閱讀 Swing 教程中關于如何使用按鈕的部分,了解一些基本示例以幫助您入門。
那就是下載演示以查看它們是如何作業的。然后修改作業代碼以使用上面的基本邏輯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385142.html
