The examples in this article tell you about the Java voting program design code, and share it with you for your reference. The specific content is as follows
Running renderings:
The procedure is as follows:
import java.awt.*;import java.awt.Event.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.StringTokenizer;import javax.swing.*;public class VoteTest implements ActionListener{ private JFrame frame; private JPanel p1,p2,p3,p2_1; private JLabel label1,label2,label3; private JTextField name,votemess; private JTextField schoolVote[]; private JButton com,con,sure,resh,sort; private Checkbox checkbox[]; private int[] count; private int totalVote,schoolNumber,max,dis,giveup; public VoteTest(){ frame=new JFrame("China University Ranking Voting System v1.0"); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); label1=new JLabel("First enter the name of the candidate school (the number does not exceed 10, separated by commas):"); label2=new JLabel("Use the following selection box to count the votes:",JLabel.CENTER); label3=new JLabel("Election result:"); name=new JTextField(10); votemess=new JTextField(46); schoolVote=new JTextField[10]; com=new JButton("Confirm"); con=new JButton("Cancel"); sure=new JButton("Confirm"); resh=new JButton("Refresh"); sort=new JButton("Sort"); checkbox=new Checkbox[10]; p2_1=new JPanel(); count=new int[10];//Record the number of votes in the school totalVote=0; schoolNumber=0; max=3; dis=0; giveup=0; init(); } public void init(){ frame.setLayout(new GridLayout(3,1)); frame.add(p1); frame.add(p2); frame.add(p3); p1.setLayout(new BorderLayout()); p1.add(label1,BorderLayout.NORTH); p1.add(name,BorderLayout.CENTER); JPanel p1_3; p1_3=new JPanel(); p1_3.add(com); p1_3.add(con); p1_3.add(label2); p1.add(p1_3,BorderLayout.SOUTH); p2.setLayout(new BorderLayout()); JPanel p2_2; p2_2=new JPanel(); p2_1.setLayout(new GridLayout(2,5)); for(int i=0;i<=9;i++){ checkbox=new Checkbox(); p2_1.add(checkbox); } p2_2.add(sure);p2_2.add(resh);p2_2.add(sort);p2.add(p2_1,BorderLayout.CENTER);p2.add(p2_2,BorderLayout.SOUTH);p3.setLayout(new BorderLayout());JPanel p3_1,p3_2;p3_1=new JPanel();p3_2=new JPanel();p3_1.add(label3);p3_1.add(votemess);p3_2.setLayout(new GridLayout(10,1)); for(int i=0;i<=9;i++){ schoolVote=new JTextField(); p3_2.add(schoolVote); } ScrollPane scroll=new ScrollPane(); scroll.add(p3_2); p3.add(p3_1,BorderLayout.NORTH); p3.add(scroll,BorderLayout.CENTER); com.addActionListener(this); con.addActionListener(this); sure.addActionListener(this); resh.addActionListener(this); sort.addActionListener(this); } public void show(){ frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String s[]=new String[10]; if(e.getSource()==com){ p2_1.removeAll(); String s_name=name.getText(); //Extract candidate names, separated by commas (English comma or Chinese comma); StringTokenizer fenxi=new StringTokenizer(s_name,",,"); schoolNumber=fenxi.countTokens();//Get the number of candidates int i=0; while(fenxi.hasMoreTokens()){ //Represent the candidates with a radio box and add to the panel p2_1 s=fenxi.nextToken(); p2_1.add(checkbox); checkbox.setLabel(s); i++; } for(int k=0;k<schoolNumber;k++){ schoolVote[k].setText(null); } }else if(e.getSource()==con){ name.setText(null); com.setEnabled(true); for(int k=0;k<schoolNumber;k++){ schoolVote[k].setText(null); } }else if(e.getSource()==sure){ totalVote=totalVote+1; com.setEnabled(false); int number=0; for(int k=0;k<schoolNumber;k++){ if(checkbox[k].getState()){ number++; } } if(number>max){ dis++; for(int k=0;k<schoolNumber;k++){ checkbox[k].setState(false); } }else if(number==0){ giveup++; }else if(number>0&&number<=max){ for(int k=0;k<schoolNumber;k++){ if(checkbox[k].getState()){ count[k]=count[k]+1; checkbox[k].setState(false); schoolVote[k].setText(checkbox[k].getLabel()+" number of votes: "+count[k]); }else{ schoolVote[k].setText(checkbox[k].getLabel()+" number of votes: "+count[k]); } } } votemess.setText("It has counted: "+totalVote+" number of votes, and the abstention votes: "+giveup+" is invalid: "+dis); }else if(e.getSource()==sort){ for(int i=0;i<schoolNumber;i++){ for(int j=i+1;j<schoolNumber;j++){ if(count[j]>count){ String str_temp=schoolVote.getText(); schoolVote.setText(schoolVote[j].getText()); schoolVote[j].setText(str_temp); int nnn=count;count=count[j];count[j]=nnn; } } } sort.setEnabled(false); sure.setEnabled(false); }else if(e.getSource()==resh){ totalVote=0; votemess.setText("It has been counted: "+totalVote+"Zhang Votes"); name.setText(null); com.setEnabled(true); sure.setEnabled(true); sort.setEnabled(true); for(int i=0;i<=4;i++){ count=0; schoolVote.setText(null); p2_1.removeAll(); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new VoteTest().show(); } }I hope this article will be helpful to everyone to learn Java software programming.