qq_20728645 2015-06-21 09:12 采纳率: 0%
浏览 1687
已结题

java程序功能设计分析和程序基本组成框架结构 大神们帮帮忙

package com.mingrisoft.ballot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;

class Candidate extends JCheckBox { // 定义内容类,该类继承JCheckBox类
int len = 0;

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

public int getBallot(String name) {
    File file = new File("C://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("C://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 {
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(getClass()
            .getResource("0.gif")));
    candidateTwo = new Candidate("小陈", new ImageIcon(getClass()
            .getResource("1.gif")));
    candidateThree = new Candidate("小李", new ImageIcon(getClass()
            .getResource("2.gif")));
    candidateOne.setSelectedIcon(new ImageIcon(getClass().getResource(
            "0.gif")));
    candidateTwo.setSelectedIcon(new ImageIcon(getClass().getResource(
            "1.gif")));
    candidateThree.setSelectedIcon(new ImageIcon(getClass().getResource(
            "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("C://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();
}
}

  • 写回答

2条回答

  • threenewbee 2015-06-21 10:41
    关注

    你的问题是什么,这个注释很详细了,结合程序运行的界面,不难理解。如果你对swing没有概念,先google下。

    评论

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑