//MyFrame.java
import java.awt.Component;
import java.awt.Container;import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyFrame {
public void go(){
JFrame win = new JFrame("评价");
Container contentPane = win.getContentPane();
contentPane.setLayout(new GridLayout(12,1));
ArrayList <JCheckBox> cb = new ArrayList<JCheckBox>();
int length;
try{
File csvFile = new File("F:\\pingjia.csv");
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line= "";
ArrayList<Select> list = new ArrayList<Select>();
//读取题目信息
String topics = br.readLine();
JLabel labOne = new JLabel(topics,JLabel.CENTER);
contentPane.add(labOne);
topics = topics.replace(",",":");
//读取选项信息
while((line = br.readLine())!=null){
StringTokenizer st = new StringTokenizer(line,",");
List<String> list2 = new ArrayList<String>();
while((st.hasMoreTokens())){
list2.add(st.nextToken());
}
Select select = new Select(list2);
list.add(select);
}
br.close();
//生成迭代器
length = list.size();
Shuffer sf = new Shuffer(length);
Iterator<Integer> iteshuf = sf.iterator();
do{
Select select = list.get((Integer) iteshuf.next());
JCheckBox temp = new JCheckBox(select.getDes());
cb.add(temp);
if(select.getAnser()){
temp.setSelected(true);
}
}while(iteshuf.hasNext());
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
JButton btn = new JButton("提交");
btn.setSize(10, 20);
Iterator<JCheckBox> it = cb.iterator();
while(it.hasNext()){
contentPane.add((Component) it.next());
}
contentPane.add(btn);
win.setLocation(400, 200);
win.setSize(400, 400);
win.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame mf = new MyFrame();
mf.go();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//IteShuf.java
import java.util.Iterator;
public class IteShuf implements Iterator<Integer> {
private int curpos;
private int[] cards;
public IteShuf(int[] cards){
curpos = 0;
this.cards = cards;
}
public boolean hasNext(){
//返回boolean值,true为还有下一个,false为无下一个
return ++curpos < cards.length;
}
public Integer next(){
//返回当前的cards
return cards[curpos];
}
public void remove(){
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Select.java
import java.util.List;
public class Select {
private String description;
private boolean answer = true;
public Select(List<String> list){
//保存答案
this.description = list.get(0);
//保存答案标记
if(list.get(1).equals("1")){
this.answer = true;
}
else{
this.answer = false;
}
}
public String getDes(){
return this.description;
}
public boolean getAnser(){
return answer;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
//Shuffer.java
import java.util.Iterator;
import java.util.Random;
public class Shuffer {
private int[] cards;
public Shuffer(int length){
cards = new int[length];
for(int i=0; i<length; i++){
cards[i] = i;
}
//生成随机数
Random rd = new Random();
for(int i=length-1; i>0; --i){
int j = rd.nextInt(length-1);
int t =cards[i];
cards[i] = cards[j];
cards[j] = t;
}
}
public int items(int i){
return cards[i];
}
//迭代器生成函数
public Iterator<Integer> iterator(){
return new IteShuf(cards);
}
}
------------------------------------------------------------------------------------------------------
//csv
请选择您的评价结果:
*****,讲解透彻生动,好评,1
授课差评!,0
授课差差评!,0
*****,培训组织,最好评!,1
培训组织,差评!,0
培训环境,好评!,1
培训环境,差评!,0
*****,收获很大,好评!,1
培训没有效果,白白浪费时间!,0