java實(shí)現(xiàn)投票程序設(shè)計(jì)
本文實(shí)例為大家講述了java實(shí)現(xiàn)投票程序設(shè)計(jì)代碼,分享給大家供大家參考,具體內(nèi)容如下
運(yùn)行效果圖:
程序如下:
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("中國(guó)大學(xué)排行榜選票系統(tǒng)v1.0"); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); label1=new JLabel("首先輸入候選學(xué)校的名字(數(shù)量不超過(guò)10,名字之間用逗號(hào)分隔):"); label2=new JLabel("用下面的選擇框統(tǒng)計(jì)選票:",JLabel.CENTER); label3=new JLabel("選舉結(jié)果:"); name=new JTextField(10); votemess=new JTextField(46); schoolVote=new JTextField[10]; com=new JButton("確認(rèn)"); con=new JButton("取消"); sure=new JButton("確定"); resh=new JButton("刷新"); sort=new JButton("排序"); checkbox=new Checkbox[10]; p2_1=new JPanel(); count=new int[10];//記錄學(xué)校的選票數(shù) 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(); //提取候選的名字,名字用逗號(hào)(英文逗號(hào)或漢文逗號(hào))分隔; StringTokenizer fenxi=new StringTokenizer(s_name,",,"); schoolNumber=fenxi.countTokens();//獲取候選的個(gè)數(shù) 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()+"的得票數(shù):"+count[k]); }else{ schoolVote[k].setText(checkbox[k].getLabel()+"的得票數(shù):"+count[k]); } } } votemess.setText("己統(tǒng)計(jì)了:"+totalVote+"張選票,其中棄權(quán)票:"+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("己統(tǒng)計(jì)了:"+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(); } }
希望本文所述對(duì)大家學(xué)習(xí)java軟件編程有所幫助。
相關(guān)文章
解讀RedisTemplate的各種操作(set、hash、list、string)
這篇文章主要介紹了解讀RedisTemplate的各種操作(set、hash、list、string),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12java實(shí)用小技巧之判斷l(xiāng)ist是否有重復(fù)項(xiàng)簡(jiǎn)單例子
這篇文章主要給大家介紹了關(guān)于java實(shí)用小技巧之判斷l(xiāng)ist是否有重復(fù)項(xiàng)的相關(guān)資料,在開(kāi)發(fā)工作中我們有時(shí)需要去判斷List集合中是否含有重復(fù)的元素,需要的朋友可以參考下2023-10-10Spring Boot 2.X快速整合jpa過(guò)程解析
這篇文章主要介紹了Spring Boot 2.X 如何快速整合jpa?,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08BigDecimal divide除法除不盡報(bào)錯(cuò)的問(wèn)題及解決
這篇文章主要介紹了BigDecimal divide除法除不盡報(bào)錯(cuò)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06詳解springboot項(xiàng)目啟動(dòng)時(shí)如何排除用不到的bean
使用springboot開(kāi)發(fā)項(xiàng)目,我們有時(shí)候會(huì)排除一些項(xiàng)目里面用不到的bean,不然的話(huà)項(xiàng)目啟動(dòng)會(huì)報(bào)錯(cuò),這種情況通常是發(fā)生在什么場(chǎng)景里呢,以及如何解決呢,今天咱們就聊一聊2024-01-01Spring實(shí)現(xiàn)Quartz自動(dòng)配置的方法詳解
這篇文章主要介紹了Spring實(shí)現(xiàn)Quartz自動(dòng)配置的方法詳解,如果想在應(yīng)用中使用Quartz任務(wù)調(diào)度功能,可以通過(guò)Spring Boot實(shí)現(xiàn)Quartz的自動(dòng)配置,以下介紹如何開(kāi)啟Quartz自動(dòng)配置,以及Quartz自動(dòng)配置的實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2023-11-11