本文實例為大家講述了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軟件編程有所幫助。