Java Implementation Voting program design _java

Source: Internet
Author: User

This example for you to tell the Java implementation of the voting program design code, to share with you for your reference, the specific contents are as follows

Run Effect chart:

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 Rankings ballot System v1.0");
        P1=new JPanel ();
        P2=new JPanel ();
        
        P3=new JPanel ();
        Label1=new JLabel ("First enter the name of the candidate school (number not exceeding 10, separated by commas):");
        Label2=new JLabel ("Statistical ballot with the following selection box:", Jlabel.center);
        
        Label3=new JLabel ("Election results:");
        Name=new JTextField (10);
        Votemess=new JTextField (46);
   Schoolvote=new JTEXTFIELD[10];     
        Com=new JButton ("confirmation");
        Con=new JButton ("cancellation");
        Sure=new JButton ("OK");
        Resh=new JButton ("refresh");
        
        Sort=new JButton ("sort");
        Checkbox=new CHECKBOX[10];
        
        P2_1=new JPanel ();
        Count=new int[10];//records the number of school votes 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);p 1_3.add (Con);p 1_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);p 2_2.add (resh);p 2_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);p 3_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);p 3.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 ();
            Extracts candidate names, separated by commas (English comma or comma), StringTokenizer fenxi=new StringTokenizer (S_name, ",,");
            Schoolnumber=fenxi.counttokens ()///Get the number of candidates int i=0;
                while (Fenxi.hasmoretokens ()) {//The candidate is represented by a single marquee and added 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 () + "The number of votes:" +count[k]);         }else{Schoolvote[k].settext (Checkbox[k].getlabel () + "Number of votes:" +count[k]);
        }} votemess.settext ("Has counted:" "+totalvote+" Zhang votes, which abstained: "+giveup+" Invalid Ticket: "+dis); }else if (E.getsource () ==sort) {for (int i=0;i<schoolnumber;i++) {for (int j=i+1;j<scho
                        olnumber;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 ("has already counted:" +totalvote+ "Zhang Ballot");
            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-ge
    nerated method stub the new Votetest (). Show ();
 }    

}

Hopefully this article will help you learn about Java software programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.