import java.awt.*;import java.awt.event.*;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;
import javax.swing.*;
public class ElectToo extends JFrame implements ActionListener,ItemListener{static ElectToo frm=new ElectToo();static Save sa=new Save();static JLabel lab1=new JLabel("总人数:");static JLabel lab2=new JLabel("最高票数选手:");static JLabel lab3=new JLabel("最高票数:");static JDialog dg=new JDialog();static JLabel lab4=new JLabel();static JLabel lab5=new JLabel();static JLabel lab6=new JLabel();static JLabel lab7=new JLabel();static JLabel lab8=new JLabel();static JLabel lab9=new JLabel();static JLabel lab10=new JLabel();static JLabel lab11=new JLabel();static JLabel lab12=new JLabel();static JLabel lab13=new JLabel();static JButton btn1=new JButton("提交");static JButton btn2=new JButton("刷新");static JButton btn3=new JButton("保管");static JButton btn4=new JButton("取消");static TextField tf1=new TextField("",5);static TextField tf2=new TextField("",15);static TextField tf3=new TextField("",5);static JCheckBox jb1=new JCheckBox("陈明茂");static JCheckBox jb2=new JCheckBox("张三");static JCheckBox jb3=new JCheckBox("李四");static JCheckBox jb4=new JCheckBox("王五");static JCheckBox jb5=new JCheckBox("张三丰");static JCheckBox jb6=new JCheckBox("乔峰");static JCheckBox jb7=new JCheckBox("萧十一郎");static JCheckBox jb8=new JCheckBox("王老吉");static JCheckBox jb9=new JCheckBox("费德南");static JCheckBox jb10=new JCheckBox("林无敌");static JPanel pan1=new JPanel();static JPanel pan2=new JPanel();static JPanel pan3=new JPanel();static JPanel pan4=new JPanel();static int count=0;static int count1=0;static int x1=100,x2=100,x3=100,x4=100,x5=100,x6=100,x7=100,x8=100,x9=100,x10=100;static int y=250;static int z=250;/** * @param args */public static void main(String[] args) { // TODO 自动生成方法存根 BorderLayout br=new BorderLayout(); frm.setLayout(br); frm.addWindowListener(sa); frm.setTitle("投票计数器"); frm.add(pan1,br.NORTH); pan1.setBounds(0, 0, 500, 10); pan1.add(lab1); pan1.add(tf1); pan1.add(lab2); pan1.add(tf2); pan1.add(lab3); pan1.add(tf3); frm.add(pan2,br.WEST); GridLayout gl=new GridLayout(10,1,0,3); pan2.setLayout(gl); pan2.add(jb1); pan2.add(jb2); pan2.add(jb3); pan2.add(jb4); pan2.add(jb5); pan2.add(jb6); pan2.add(jb7); pan2.add(jb8); pan2.add(jb9); pan2.add(jb10); frm.add(pan3,br.SOUTH); pan3.setBounds(150, 450, 10, 10); pan3.add(btn1); pan3.add(btn2); frm.add(pan4,br.EAST); GridLayout gl1=new GridLayout(10,1,0,3); pan4.setLayout(gl1); pan4.add(lab4); lab4.setForeground(new Color(0,0,255)); pan4.add(lab5); lab5.setForeground(new Color(0,0,255)); pan4.add(lab6); lab6.setForeground(new Color(0,0,255)); pan4.add(lab7); lab7.setForeground(new Color(0,0,255)); pan4.add(lab8); lab8.setForeground(new Color(0,0,255)); pan4.add(lab9); lab9.setForeground(new Color(0,0,255)); pan4.add(lab10); lab10.setForeground(new Color(0,0,255)); pan4.add(lab11); lab11.setForeground(new Color(0,0,255)); pan4.add(lab12); lab12.setForeground(new Color(0,0,255)); pan4.add(lab13); lab13.setForeground(new Color(0,0,255)); frm.setSize(500, 400); frm.setLocation(400, 300);// frm.setDefaultCloseOperation(EXIT_ON_CLOSE); frm.setVisible(true); frm.setResizable(false); tf1.setEditable(false); tf2.setEditable(false); tf3.setEditable(false); btn1.addActionListener(frm); btn2.addActionListener(frm); btn3.addActionListener(frm); btn4.addActionListener(frm); jb1.addItemListener(frm); jb2.addItemListener(frm); jb3.addItemListener(frm); jb4.addItemListener(frm); jb5.addItemListener(frm); jb6.addItemListener(frm); jb7.addItemListener(frm); jb8.addItemListener(frm); jb9.addItemListener(frm); jb10.addItemListener(frm); btn1.setEnabled(false);}static class Save extends WindowAdapter{public void windowClosing(WindowEvent e){dg.setTitle("保管文件?");dg.setSize(200, 100);dg.add(btn3);dg.add(btn4);dg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30));dg.setLocation(400, 300);dg.setVisible(true);} }public void itemStateChanged(ItemEvent e){ if(count10){//每个人至少投两个人 btn1.setEnabled(true); }else{ btn1.setEnabled(false); } if(count15){ //此投票计数器一个人只能投四票 if(jb1.isSelected()){ count1++; jb1.setEnabled(true); } if(jb2.isSelected()){ count1++; jb2.setEnabled(true); } if(jb3.isSelected()){ count1++; jb3.setEnabled(true); } if(jb4.isSelected()){ count1++; jb4.setEnabled(true); } if(jb5.isSelected()){ count1++; jb5.setEnabled(true); } if(jb6.isSelected()){ count1++; jb6.setEnabled(true); } if(jb7.isSelected()){ count1++; jb7.setEnabled(true); } if(jb8.isSelected()){ count1++; jb8.setEnabled(true); } if(jb9.isSelected()){ count1++; jb9.setEnabled(true); } if(jb10.isSelected()){ count1++; jb10.setEnabled(true); } }else{ count1=0; if(jb1.isSelected()){ jb1.setEnabled(true); }else { jb1.setEnabled(false); } if(jb2.isSelected()){ jb2.setEnabled(true); }else { jb2.setEnabled(false); } if(jb3.isSelected()){ jb3.setEnabled(true); }else { jb3.setEnabled(false); } if(jb4.isSelected()){ jb4.setEnabled(true); }else { jb4.setEnabled(false); } if(jb5.isSelected()){ jb5.setEnabled(true); }else { jb5.setEnabled(false); } if(jb6.isSelected()){ jb6.setEnabled(true); }else { jb6.setEnabled(false); } if(jb7.isSelected()){ jb7.setEnabled(true); }else { jb7.setEnabled(false); } if(jb8.isSelected()){ jb8.setEnabled(true); }else { jb8.setEnabled(false); } if(jb9.isSelected()){ jb9.setEnabled(true); }else { jb9.setEnabled(false); } if(jb10.isSelected()){ jb10.setEnabled(true); }else { jb10.setEnabled(false); } }}public void actionPerformed(ActionEvent e){ JButton btn=(JButton)e.getSource(); if(btn==btn1){ btn1.setEnabled(false); count++; tf1.setText(""+count); Graphics g=getGraphics(); g.setColor(Color.RED); if(jb1.isSelected()){ g.fillRect(x1,70, 1, 15); x1++; } if(jb2.isSelected()){ g.fillRect(x2,98, 1, 15); x2++; } if(jb3.isSelected()){ g.fillRect(x3,126, 1, 15); x3++; } if(jb4.isSelected()){ g.fillRect(x4,158, 1, 15); x4++; } if(jb5.isSelected()){ g.fillRect(x5,188, 1, 15); x5++; } if(jb6.isSelected()){ g.fillRect(x6,218, 1, 15); x6++; } if(jb7.isSelected()){ g.fillRect(x7,y, 1, 15); x7++; } if(jb8.isSelected()){ g.fillRect(x8,278, 1, 15); x8++; } if(jb9.isSelected()){ g.fillRect(x9,308, 1, 15); x9++; } if(jb10.isSelected()){ g.fillRect(x10,338, 1, 15); x10++; } int array[]={x1-100,x2-100,x3-100,x4-100,x5-100,x6-100,x7-100,x8-100,x9-100,
美袭来护肤品旗舰店x10-100}; lab4.setText(array[0]+"票"); lab5.setText(array[1]+"票"); lab6.setText(array[2]+"票"); lab7.setText(array[3]+"票"); lab8.setText(array[4]+"票"); lab9.setText(array[5]+"票"); lab10.setText(array[6]+"票"); lab11.setText(array[7]+"票"); lab12.setText(array[8]+"票"); lab13.setText(array[9]+"票"); java.util.Arrays.sort(array); tf3.setText(""+array[9]); if(array[9]==(x1-100)){ tf2.setText("陈明茂"); } if(array[9]==(x2-100)){ tf2.setText("张三"); } if(array[9]==(x3-100)){ tf2.setText("李四"); } if(array[9]==(x4-100)){ tf2.setText("王五"); } if(array[9]==(x5-100)){ tf2.setText("张三丰"); } if(array[9]==(x6-100)){ tf2.setText("乔峰"); } if(array[9]==(x7-100)){ tf2.setText("萧十一郎"); } if(array[9]==(x8-100)){ tf2.setText("王老吉"); } if(array[9]==(x9-100)){ tf2.setText("费得南"); }if(array[9]==(x10-100)){ tf2.setText("林无敌"); } if(array[9]==array[8]||array[9]==array[7]||array[9]==array[6]||array[9]==array[5]||array[9]==array[4]||array[9]==array[3]||array[9]==array[2]||array[9]==array[1]||array[9]==array[0]){ tf2.setText("并立第一,请看图!"); } if(jb1.isSelected()){ jb1.setEnabled(true); }else { jb1.setEnabled(false); } if(jb2.isSelected()){ jb2.setEnabled(true); }else { jb2.setEnabled(false); } if(jb3.isSelected()){ jb3.setEnabled(true); }else { jb3.setEnabled(false); } if(jb4.isSelected()){ jb4.setEnabled(true); }else { jb4.setEnabled(false); } if(jb5.isSelected()){ jb5.setEnabled(true); }else { jb5.setEnabled(false); } if(jb6.isSelected()){ jb6.setEnabled(true); }else { jb6.setEnabled(false); } if(jb7.isSelected()){ jb7.setEnabled(true); }else { jb7.setEnabled(false); } if(jb8.isSelected()){ jb8.setEnabled(true); }else { jb8.setEnabled(false); } if(jb9.isSelected()){ jb9.setEnabled(true); }else { jb9.setEnabled(false); } if(jb10.isSelected()){ jb10.setEnabled(true); }else { jb10.setEnabled(false); } }else if(btn==btn2){ jb1.setSelected(false); jb2.setSelected(false); jb3.setSelected(false); jb4.setSelected(false); jb5.setSelected(false); jb6.setSelected(false); jb7.setSelected(false); jb8.setSelected(false); jb9.setSelected(false); jb10.setSelected(false); jb1.setEnabled(true); jb2.setEnabled(true); jb3.setEnabled(true); jb4.setEnabled(true); jb5.setEnabled(true); jb6.setEnabled(true); jb7.setEnabled(true); jb8.setEnabled(true); jb9.setEnabled(true); jb10.setEnabled(true); } else if(btn==btn3){ JFileChooser jf=new JFileChooser(); jf.showSaveDialog(ElectToo.this); String filename=jf.getSelectedFile().getAbsolutePath().trim(); FileWriter fw; BufferedWriter bw; try{ fw=new FileWriter(filename+".txt"); bw=new BufferedWriter(fw); String array[]={jb1.getText(),":", lab4.getText(),"\r\n",jb2.getText(),":", lab5.getText(),"\r\n",jb3.getText(),":", lab6.getText(),"\r\n",jb4.getText(),":", lab7.getText(),"\r\n",jb5.getText(),":", lab8.getText(),"\r\n",jb6.getText(),":", lab9.getText(),"\r\n",jb7.getText(),":", lab10.getText(),"\r\n",jb8.getText(),":", lab11.getText(),"\r\n",jb9.getText(),":", lab12.getText(),"\r\n",jb10.getText(),":", lab13.getText()}; for(int i=0;iarray.length;i++){ fw.write(array
); } fw.close(); JOptionPane.showMessageDialog(null,"保管成功") ; dg.dispose(); } catch(IOException ioe) { JOptionPane.showMessageDialog(null,"保管失败") ; } }else if(btn==btn4){ dg.dispose(); }}}