1、 private int id; /候选者的编号 private int num; / 得票数量 private double voteResult; /得票率 private boolean isVote; /是否中选 Candidate(String name, int id) ;/Candidate 的构造方法 public String getName() ;public void setName(String name) ; public int getId() ; public void setId(int id) ; public int getNum() ; public vo
2、id setNum(int num) ; public double getVoteResult() ; public void setVoteResult(double voteResult) ; public boolean isVote(); public void setVote(boolean isVote) ;2.2.2、管理类:Manager类public class Manager public int candidates; / 候选人的数量 public int percentage; / 当选标准(百分比) public int voters = 0; / 投票人数量 L
3、ist listc ;/ 装载候选者的容器 public static void main(String args) ; public void calculateResult() ;/ 计算结果 2.2.3、窗口的操作类UserInterface public class UserInterface extends JFrame private static final long serialVersionUID = 1L; public Container contentPanel; / 当前界面控制器 public Manager manager; /利于数据通信,主要包括:候选人数量、
4、当选标准、投票者数量 public JPanel p1; /输入信息的面板 public JPanel p2; /投票面板 public JPanel p3; /显示投票结果的面板 public List list_textfield ;/装载候选人姓名的容器JRadioButton list_jradiobutton ;/投票 public UserInterface(Manager m) ; public void update() ;/窗口中增删组建后立即更新 public void addCandi(int num);/panel1输入侯选者姓名完成后,生成候选者对象 2.2.4、输入
5、面板类:MainGraphpublic class MainGraph private UserInterface userInterface; /与主面板的通信 public MainGraph(UserInterface userInterface) ;/MainGraph的构造函数 public void launchMainGraph() ;/加载主面板2.2.5、投票面板类:VoteGraph public class VoteGraph /与主面板的通信 public VoteGraph(UserInterface userInterface) ;/构造函数 public void
6、 launchVoteGraph();/加载投票面板2.2.6、显示结果类:ResultGraphpublic class ResultGraph public ResultGraph(UserInterface userInterface) ;/构造函数 public void launchResultGraph() ;/加载显示结果的面板3 功能实现 3.1 窗口显示点击运行后弹出一个窗口(Panel1),要求用户输入候选人的个数(1-12)以及当选标准(若为50%,只需输入50即可) 假若依次输入12, 50.点击确定后,窗口中会提示输入侯选人的姓名,候选人的编号是从小到大依次生成的。
7、依次输入12人的姓名点击提交后,就会弹出投票的窗口假若共有7名投票人参与投票(每次投票完点击提交即为完成依次投票)投票过程如下:投票者1:1号:吴红岩 3号:邵芳 9号:李丹 11号:李四投票者2: 2号:刘莉莉 3号:邵芳 4号:尹聪敏 6号:王小丽 8号:王永霞 投票者3: 1号:吴红岩 2号:刘莉莉 4号:尹聪敏 5号:张三 7号:李泽 9号:李丹投票者4:吴红岩 7号:李泽 12号:王龙贺 投票者5: 3号:张三 7号:李泽10号:王小慧投票者6: 10号:王小慧 11号:李四 12号:王龙贺投票者7:尹聪敏 最终的投票结果如下图所示: 3.2 功能1 整个系统的UML时序图如下图所示
8、:附录:程序源代码1、Candidate 的详细代码: Candidate(String name, int id) this.name = name; this.id = id; this.num = 0; this.voteResult = 0; this.isVote = false; public String getName() return name; public void setName(String name) public int getId() return id; public void setId(int id) public int getNum() return
9、num; public void setNum(int num) this.num = num; public double getVoteResult() return voteResult; public void setVoteResult(double voteResult) this.voteResult = voteResult; public boolean isVote() return isVote; public void setVote(boolean isVote) this.isVote = isVote; public String toString() retur
10、n this.name;2、Manager类的详细代码import java.util.ArrayList;import java.util.List; listc = new ArrayList(); public static void main(String args) Manager m = new Manager(); UserInterface ui = new UserInterface(m); MainGraph mainGraph = new MainGraph(ui); mainGraph.launchMainGraph(); public void calculateRe
11、sult() double d; for (int i = 0; i = percentage * 0.01) c.setVote(true); 3、UserInterface类的详细代码:import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Iterator;import java.util.Vector;import javax.sw
12、ing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.table.DefaultTableCellRenderer;/* * *
13、author why * */ list_textfield = new ArrayList list_jradiobutton = new ArrayList= this.getHeight() - 250) y = yy; x1 = x1 + 300; x2 = x2 + 300; y += 50; text_name = new JTextField(); / 新建输入姓名的文本框 label_name = new JLabel(ss); label_name.setText(候选人 + i + 的姓名: label_name.setBounds(x1, y, w, h); text_n
14、ame.setBounds(x2, y, w, h); list_textfield.add(text_name); / 把输入姓名的文本框加入到list中 p1.add(label_name); p1.add(text_name); this.update(); submit.setBounds(this.getWidth() / 3, this.getHeight() - 150, 100, 20); submit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) for (i
15、nt i = 0; list_textfield.size(); String name = list_textfield.get(i).getText(); / 得到文本框中输入的候选者姓名 Candidate candi = new Candidate(name, i + 1); / 根据输入的姓名生成一个候选者 manager.listc.add(candi); / 把候选者加入到list中 VoteGraph voteGraph = new VoteGraph(UserInterface.this); voteGraph.launchVoteGraph(); / 加载投票面板(Pane
16、l2) ); p1.add(submit); 4、MainGraph类的详细代码:import java.awt.HeadlessException; * 系统的主界面,接受候选者人数,以及当选的标准 public MainGraph(UserInterface userInterface) super(); this.userInterface = userInterface; public void launchMainGraph() userInterface.p1 = new JPanel(); userInterface.p1.setLayout(null); userInterfa
17、ce.p1.setBackground(Color.orange); userInterface.contentPanel.add(userInterface.p1); JLabel lab = new JLabel(请输入投票信息 lab.setBounds(200, 30, 250, 50); int size = 25; lab.setFont(new Font(Serif, Font.PLAIN, size); JLabel label = new JLabel(请输入候选人的个数(1-12): final JTextField text = new JTextField(); / 输
18、入候选人的个数 JLabel labelp = new JLabel(请输当选标准(百分比): final JTextField pertage = new JTextField(); / 输入当选百分比 JButton b = new JButton(确定 / 对候选人个数与百分比进行计较的按钮 label.setBounds(80, 100, 170, 20); labelp.setBounds(80, 130, 150, 20); text.setBounds(240, 100, 150, 20); pertage.setBounds(240, 130, 150, 20); b.setB
19、ounds(500, 130, 70, 20); b.addActionListener(new ActionListener() userInterface.manager.candidates = Integer.parseInt(text.getText(); / 得到候选者的个数 userInterface.addCandi(userInterface.manager.candidates); userInterface.manager.percentage = Integer.parseInt(pertage.getText(); / 得到当选百分比 userInterface.p1.add(label); userInterface.p1.add(lab); userInterface.p1.add(text); userInterface.p1.add(b); userInterface.p1.add(labelp); userInterface.p1.add(pertage); userInterface.update();5、VoteGraph类的详细代码:public class VoteGraph public VoteGraph(UserInterface userInterface) this.userInterf
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2