关闭

java写的简单选票统计程序

958人阅读 评论(0) 收藏 举报

功能很简单的一个例子,和大家一起分享一下,同为java爱好者,一起学习进步哦。

InputPerson.java

import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
public class InputPerson extends Panel implements ActionListener
{
 TextField inputPeopleName;
 PersonSelected personSelected[];//存放候选人的数组
 Button button;
 InputPerson(PersonSelected personSelected[])
 {
  this.personSelected=personSelected;
  add(new Label("请输入候选人名字,用逗号分隔"));
  inputPeopleName=new TextField(16);
  add(inputPeopleName);
  button=new Button("确定");
  add(button);
  button.addActionListener(this);
 }
 public void actionPerformed(ActionEvent e)
 {
  String tempStr=inputPeopleName.getText();
  if(tempStr.length()>0)
  {
   StringTokenizer token=new StringTokenizer(tempStr,",,");
   personSelected=new PersonSelected[token.countTokens()];
   int k=0;
   while(token.hasMoreTokens())
   {
    String temp=token.nextToken();
    personSelected[k]=new PersonSelected(temp,0);
    personSelected[k].setName(temp);
    k++;
   }
  }
  else
  {
   inputPeopleName.setText("请输入名字,并用逗号分隔");
  }
 }
public PersonSelected[] getPersonSelected()
{
 return personSelected;
}
}

 

PersonSelected.java
import java.awt.*;
public class PersonSelected extends Panel
{
 String name;//候选人的名字
 int count;//得票数
 Checkbox box;//代表候选人外观的组件
 public PersonSelected(String name,int count)
 {
  this.name=name;
  this.count=count;
  box=new Checkbox();
  add(box);
 }
 public void setName(String name)
 {
  this.name=name;
  box.setLabel(name);
 }
 public String getName()
 {
  return name;
 }
 public void addCount()
 {
  count++;
 }
 public int getCount()
 {
  return count;
 }
 public Checkbox getBox()
 {
  return box;
 }
}

ResultArea.java

import java.awt.*;
public class ResultArea extends Panel
{
 PersonSelected personSelected[]; //存放候选人的数组
 TextArea text;
 ResultArea(PersonSelected personSelected[])
 {
  this.personSelected=personSelected;
  text=new TextArea(12,40);
  text.setText(null);
  add(text);
 }
public void setPersonSelected(PersonSelected personSelected[])
{
 text.setText(null);
 String str[]=new String[personSelected.length];
 int count[]=new int[personSelected.length];
 for(int k=0;k<str.length;k++)
 {
  str[k]=personSelected[k].getName();
  count[k]=personSelected[k].getCount();
 }
 for(int k=0;k<str.length;k++)
 {
  for(int i=k+1;i<str.length;i++)
   if(count[i]>count[k])
   {
    String temp=str[k];
    int n=count[k];
    str[k]=str[i];
    str[i]=temp;
    count[i]=n;
   }
 }
 for(int k=0;k<str.length;k++)
 {
  text.append("\n"+str[k]+"得票:"+count[k]);
 }
 validate();
}
}

SelectPane.java

import java.awt.*;
import java.awt.event.*;
public class SelectPane extends Panel implements ActionListener,ItemListener
{
 Label label=new Label("请将你推选的人选中:");
 int maxSelectedNumber;  //一张选票可推选的最多人数
 int 弃权票数;
 int totalVote;
 PersonSelected personSelected[];//存放候选人的数组
 Button button;
 SelectPane(PersonSelected personSelected[],int max)
 {
  this.personSelected=personSelected;
  maxSelectedNumber=max;
  button=new Button("确认");
  button.addActionListener(this);
  init();
 }
 public void init()
 {
  removeAll();
  add(label);
  for(int k=0;k<personSelected.length;k++)
  {
   personSelected[k].getBox().addItemListener(this);
   add(personSelected[k].getBox());
  }
  add(button);
  validate();
 }
 public void actionPerformed(ActionEvent e)
 {
  totalVote=totalVote+1; //记录下统计的票数
  //检查选票上被推选的人数
  int number=0;
  for(int k=0;k<personSelected.length;k++)
  {
   if(personSelected[k].getBox().getState())
   {
    number++;
   }
  }
  if(number==0)
  {
   弃权票数++;
  }
  else
  {
   for(int k=0;k<personSelected.length;k++)
   {
    if(personSelected[k].getBox().getState())
    {
     personSelected[k].addCount();
     personSelected[k].getBox().setState(false);
    }
   }
  }
  label.setText("已统计了:"+totalVote+"张选票,其中弃权票:"+弃权票数);
  validate();
 }
 public void itemStateChanged(ItemEvent e)
 {
  Checkbox box=(Checkbox)e.getItemSelectable();
  //检查选票是否符合规定的最多可推选的人数
  int number=0;
  for(int k=0;k<personSelected.length;k++)
  {
   if(personSelected[k].getBox().getState())
   {
    number++;
   }
  }
  if(number>maxSelectedNumber)
  {
   box.setState(false);  //不允许推选该人
  }
 }
 public PersonSelected [] getPersonSelected()
 {
  return personSelected;
 }
 public void setPersonSelected(PersonSelected personSelected[])
 {
  this.personSelected=personSelected;
 }
}

 

Start.java
import java.awt.*;
import java.awt.event.*;
class SelectWindow extends Frame implements ActionListener
{
 PersonSelected personSelected[];
 InputPerson input;
 SelectPane select;
 ResultArea show;
 
 Button buttonInput,buttonSelect,buttonResult,reNew;
 int max=3;
 CardLayout card;
 Panel center=new Panel(),
       south=new Panel();
 SelectWindow()
 {
  personSelected=new PersonSelected[1];
  for(int k=0;k<personSelected.length;k++)
  {
   personSelected[k]=new PersonSelected("无名",0);
  }
  card=new CardLayout();
  center.setLayout(card);
  input=new InputPerson(personSelected);
  select=new SelectPane(personSelected,max);
  show=new ResultArea(personSelected);
  center.add("001",new Label("选举程序",Label.CENTER));
  center.add("input",input);
  center.add("select",select);
  center.add("show",show);
  buttonInput=new Button("输入候选人界面");
  buttonInput.addActionListener(this);
  buttonSelect=new Button("统计选票界面,最多选"+max+"人");
  buttonSelect.addActionListener(this);
  buttonResult=new Button("查看得票界面");
  buttonResult.addActionListener(this);
     reNew=new Button("重新选举");
     reNew.addActionListener(this);
     south.add(buttonInput);
     south.add(buttonSelect);
     south.add(buttonResult);
     south.add(reNew);
     add(center,BorderLayout.CENTER);
     add(south,BorderLayout.SOUTH);
     setSize(450,200);
     setVisible(true);
     validate();
 }
 public void actionPerformed(ActionEvent e)
 {
  Button b=(Button)e.getSource();
  setTitle(b.getLabel());
  if(b==buttonInput)
  {
   card.show(center,"input");
   //切换到输入界面,以便修改personSelected数组
   buttonInput.setEnabled(false);
  }
  if(b==buttonSelect)
  {
   personSelected=input.getPersonSelected();
      //得到被输入界面修改后的数组
   select.setPersonSelected(personSelected);
   select.init();
   card.show(center,"select");
  }
  if(b==buttonResult)
  {
   personSelected=select.getPersonSelected();
   //得到被选举界面修改后的数组
   show.setPersonSelected(personSelected);
   card.show(center,"show");
  }
  if(b==reNew)
  {
   personSelected=new PersonSelected[1];
   //数组的初始大小是1
   for(int k=0;k<personSelected.length;k++)
   {
    personSelected[k]=new PersonSelected("无名",0);
   }
   center.removeAll();
   input=new InputPerson(personSelected);
   select=new SelectPane(personSelected,3);
   //每张选票上最多推选3人
   show=new ResultArea(personSelected);
   center.add("001",new Label("选票程序",Label.CENTER));
   center.add("input",input);
   center.add("select",select);
   center.add("show",show);
   card.show(center,"001");
   buttonInput.setEnabled(true);
   center.validate();
  }
 }
}
public class Start {
 public static void main(String args[])
 {
  SelectWindow win=new SelectWindow();
  win.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });
 }
}

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15447次
    • 积分:334
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条
    文章存档
    最新评论