package gui;
import data.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MainFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JComboBox comboBox[];
private Console console;
private Data data;
private Paper onHand;
private JButton OK;
private JButton Cancel;
private JButton submit;
private JButton back;
public MainFrame() {
super("夏日凉风---by 刘畅");
data = new Data();
onHand = new Paper();
String str_0[] = {"A.男","B.女"};
String str_1[] = {"A.18岁以下","B.18~30","C.30~40","D.40~50","E.50以上"};
String str_2[] = {"A.小学","B.初中","C.高中","D.大专.","E.大学"};
String str_3[] = {"A.1000以下","B.1000~2000","C.2000~3000","D.3000以上"};
String str_4[] = {"A.诺基亚","B.摩托罗拉","C.索爱","D.三星","E.国产手机","F.其他"};
String str_5[] = {"A.500以下","B.500~1500","C.1500~2500","D.2500~3500","E.3500以上"};
String str_6[] = {"A.价格便宜","B.外形新颖","C.音乐手机","D.商务手机","E.智能手机","F.其他"};
String str_7[] = {"A.50以下","B.50~100","C.100~200","D.200~300","E.300以上"};
String str_8[] = {"A.1年以内","B.1年~2年","C.2年~3年","D.3年以上"};
String str_9[] = {"A.有新机型","B.手机出了问题但还能够使用","C.手机无法再使用","D.其他"};
String choiceString[][] = {str_0,str_1,str_2,str_3,
str_4,str_5,str_6,str_7,str_8,str_9};
comboBox = new JComboBox[10];
for(int i = 0; i < 10; i++) {
comboBox[i] = new JComboBox(choiceString[i]);
comboBox[i].setSelectedIndex(0);
comboBox[i].setBorder(new TitledBorder("第"+(i+1)+"题"));
}
JPanel chosePanel = new JPanel();
chosePanel.setLayout(new GridLayout(10,1));
for(int i = 0; i < 10; i++)
chosePanel.add(comboBox[i]);
JPanel actionPanel = new JPanel();
actionPanel.setLayout(new GridLayout(4,1));
OK = new JButton("保存");
Cancel = new JButton("不可用");
submit = new JButton("递交");
back = new JButton("不可用");
OK.addActionListener(this);
Cancel.addActionListener(this);
submit.addActionListener(this);
back.addActionListener(this);
actionPanel.add(submit);
actionPanel.add(OK);
actionPanel.add(back);
actionPanel.add(Cancel);
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(2,1));
console = new Console();
eastPanel.add(console);
eastPanel.add(actionPanel);
getContentPane().setLayout(new GridLayout(1,2));
getContentPane().add(chosePanel);
getContentPane().add(eastPanel);
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
int x = (screenWidth - getWidth()) / 2;
int y = (screenHeight - getHeight()) / 2;
setLocation(x, y);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void addData() {
for (int i = 0; i < 10; i++) {
onHand.setChoice(i, comboBox[i].getSelectedIndex());
}
data.add(onHand);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource() == submit) {
addData();
console.println("添加数据到"+data.getCurrentIndex()+
"处----总量: "+data.getAmount());
}else if(arg0.getSource() == OK) {
try {
//文件浏览
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("保存问卷数据");
fc.setCurrentDirectory(new File("C:\\"));
FileNameExtensionFilter filter =
new FileNameExtensionFilter("二进制文件", "bin");
fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String path = file.getAbsolutePath();
DataIO.saveData(data, path);
console.println("已保存到"+path);
}else {
}
}catch(Exception ex) {
console.println("出现异常 !");
}
}
}
public static void main(String[] args) {
MainFrame pr = new MainFrame();
pr.setVisible(true);
}
}
- 1
- 2
- 3
前往页