服务器之家

服务器之家 > 正文

java实现投票程序设计

时间:2020-03-11 13:53     来源/作者:luosijin123

本文实例为大家讲述了java实现投票程序设计代码,分享给大家供大家参考,具体内容如下

运行效果图:

java实现投票程序设计

程序如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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("中国大学排行榜选票系统v1.0");
        
        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();
        
        label1=new JLabel("首先输入候选学校的名字(数量不超过10,名字之间用逗号分隔):");
        label2=new JLabel("用下面的选择框统计选票:",JLabel.CENTER);
        label3=new JLabel("选举结果:");
        
        name=new JTextField(10);
        votemess=new JTextField(46);
        schoolVote=new JTextField[10];
        
        com=new JButton("确认");
        con=new JButton("取消");
        sure=new JButton("确定");
        resh=new JButton("刷新");
        sort=new JButton("排序");
        
        checkbox=new Checkbox[10];
        p2_1=new JPanel();
        
        count=new int[10];//记录学校的选票数
        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();
            //提取候选的名字,名字用逗号(英文逗号或汉文逗号)分隔;
            StringTokenizer fenxi=new StringTokenizer(s_name,",,");
            schoolNumber=fenxi.countTokens();//获取候选的个数
            int i=0;
            while(fenxi.hasMoreTokens()){    //用单选框代表候选,并添加到面板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()+"的得票数:"+count[k]);
                    }else{
                        schoolVote[k].setText(checkbox[k].getLabel()+"的得票数:"+count[k]);
                    }
                }
            }
            votemess.setText("己统计了:"+totalVote+"张选票,其中弃权票:"+giveup+"作废票:"+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("己统计了:"+totalVote+"张选票");
            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();
    }   
 
}

希望本文所述对大家学习java软件编程有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部