在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java语言基础 → java投票示例源码

java投票示例源码

Java语言基础

下载此实例
  • 开发语言:Java
  • 实例大小:0.01M
  • 下载次数:28
  • 浏览次数:456
  • 发布时间:2019-07-21
  • 实例类别:Java语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: java 示例 源码 投票

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

package com.mingrisoft;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.UIManager;

class Candidate extends JCheckBox { // 定义内容类,该类继承JCheckBox类
    /**
     * 
     */
    private static final long serialVersionUID = -5408876343113378593L;
    int len = 0;

    Candidate(String name, Icon icon) { // 该类包含有两个参数
        super(name, icon);
    }

    public int getBallot(String name) {
        File file = new File("D://count.txt"); // 创建文件对象
        FileReader fis;
        try {
            if (!file.exists()) // 如果该文件不存在
                file.createNewFile(); // 新建文件
            fis = new FileReader(file);
            BufferedReader bis = new BufferedReader(fis); // 创建BufferedReader对象
            String str[] = new String[3];
            String size;
            int i = 0;
            while ((size = bis.readLine()) != null) { // 循环读取文件内容
                str[i] = size.trim(); // 去除字符串中的空格
                if (str[i].startsWith(name)) {
                    int length = str[i].indexOf(":");
                    String sub = str[i].substring(length   1, str[i].length()); // 对字符串进行截取
                    len = Integer.parseInt(sub);
                    continue;
                }
                i  ;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return len;
    }

    public void addBallot(String name) { // 定义增加选票方法
        File file = new File("D://count.txt"); // 创建文件对象
        FileReader fis;
        try {
            if (!file.exists()) // 如果该文件不存在
                file.createNewFile(); // 新建文件
            fis = new FileReader(file); // 对FileReader对象进行实例化
            BufferedReader bis = new BufferedReader(fis);
            String str[] = new String[3];
            String size;
            int i = 0;
            while ((size = bis.readLine()) != null) { // 循环读取文件
                str[i] = size.trim();
                if (str[i].startsWith(name)) {
                    int length = str[i].indexOf(":"); // 获取指定字符索引位置
                    String sub = str[i].substring(length   1, str[i].length()); // 对字符串进行截取
                    len = Integer.parseInt(sub)   1;
                    break;
                }
                i  ;
            }
            FileWriter fw = new FileWriter(file); // 创建FileWriter 对象
            BufferedWriter bufw = new BufferedWriter(fw);
            bufw.write(name   ":"   len); // 向流中写数据

            bufw.close(); // 关闭流
            fw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class MyMin extends JFrame implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 7341493970991277188L;
    Box baseBox, boxH, boxV; // 创建Box对象
    JTextArea text; // 创建JTextArea对象
    JButton button; // 创建JButton对象
    Candidate candidateOne, candidateTwo, candidateThree;

    public MyMin() { // 在构造方法中设置窗体布局
        setBounds(100, 100, 500, 120);
        setVisible(true);
        setTitle("选出你心中的好干部!!");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) { // 窗体关闭事件
                System.exit(0);
            }
        });
        baseBox = Box.createHorizontalBox();
        boxH = Box.createHorizontalBox();
        boxV = Box.createHorizontalBox();
        candidateOne = new Candidate("小兵", new ImageIcon("src/images/0.gif"));
        candidateTwo = new Candidate("小陈", new ImageIcon("src/images/1.gif"));
        candidateThree = new Candidate("小李", new ImageIcon("src/images/2.gif"));
        candidateOne.setSelectedIcon(new ImageIcon("src/images/0.gif"));
        candidateTwo.setSelectedIcon(new ImageIcon("src/images/1.gif"));
        candidateThree.setSelectedIcon(new ImageIcon("src/images/2.gif"));
        boxH.add(candidateOne);
        boxH.add(candidateTwo);
        boxH.add(candidateThree);
        text = new JTextArea();
        button = new JButton("显示得票数");
        button.addActionListener(this);
        boxV.add(text);
        boxV.add(button);
        boxV.add(boxH);
        baseBox.add(boxV);
        Container con = getContentPane();
        con.setLayout(new FlowLayout());
        con.add(baseBox);
        con.validate();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        text.setText(null);
        File file = new File("D://count.txt"); // 创建文件对象
        if (!file.exists()) { // 如果该文件不存在
            try {
                file.createNewFile(); // 新建文件
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (candidateOne.isSelected()) {
            candidateOne.addBallot(candidateOne.getText());
        }
        if (candidateTwo.isSelected()) {
            candidateTwo.addBallot(candidateTwo.getText());
        }
        if (candidateThree.isSelected()) {
            candidateThree.addBallot(candidateThree.getText());
        }
        // 向文本框中追加信息
        text.append(candidateOne.getText()   ":"   candidateOne.getBallot(candidateOne.getText())   "\n");
        text.append(candidateTwo.getText()   ":"   candidateTwo.getBallot(candidateTwo.getText())   "\n");
        text.append(candidateThree.getText()   ":"   candidateThree.getBallot(candidateThree.getText())   "\n");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file); // 创建FileWriter类对象
            BufferedWriter bufw = new BufferedWriter(fw); // 创建BufferedWriter类对象
            bufw.write(text.getText()); // 将字符串数组中元素写入到磁盘文件中
            bufw.close(); // 将BufferedWriter流关闭
            fw.close();

        } catch (IOException e1) {
            e1.printStackTrace();
        }

        candidateOne.setSelected(false);
        candidateTwo.setSelected(false);
        candidateThree.setSelected(false);
    }

}

public class Ballot {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        new MyMin();
    }
}

标签: java 示例 源码 投票

实例下载地址

java投票示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警