简单JAVA投票系统
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;
import java.awt.event.*;
class MyPanel1 extends Panel implements ItemListener,ActionListener
{
Panel p1,p2,p3;
Checkbox box1,box2,box3,box4,box5;
TextArea text;
Button btVote,bttj,btCan;
CheckboxGroup group;
static String str1,str2;
static int numz,numl,numw,numa;
VoteMoniter vm;
MyPanel1()
{
p1=new Panel();
p2=new Panel();
box1=new Checkbox("张三");
box1.addItemListener(this);
box2=new Checkbox("李四");
box2.addItemListener(this);
box3=new Checkbox("王五");
box3.addItemListener(this);
p1.add(box1);
p1.add(box2);
p1.add(box3);
text=new TextArea(10,40);
btVote=new Button("投票");
btVote.addActionListener(this);
bttj=new Button("统计");
bttj.addActionListener(this);
btCan=new Button("竞选班长");
btCan.addActionListener(this);
btCan.setEnabled(false);
vm=new VoteMoniter("简单投票演示");
p2.add(btVote);
p2.add(bttj);
p2.add(btCan);
add(p1,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btVote)
{
if(box1.getState())
numz++;
if(box2.getState())
numl++;
if(box3.getState())
numw++;
btCan.setEnabled(true);
}
if(e.getSource()==bttj)
{
numa=numz+numl+numw;
text.setText(" ");
text.append("统计票数如下"+"\n\n\n");
text.append("张三的票数为:"+Integer.toString(numz)+"\n\n");
text.append("李四的票数为:"+Integer.toString(numl)+"\n\n");
text.append("王五的票数为:"+Integer.toString(numw)+"\n\n");
text.append("总票数为:"+Integer.toString(numa));
box1.setState(false);
box2.setState(false);
box3.setState(false);
}
if(e.getSource()==btCan)
{
CheckTwo();
vm.setVisible(true);
vm.box1.setLabel(str1);
vm.box2.setLabel(str2);
text.setText("");
}
}
public void CheckTwo()
{
if((numz>numw)&&(numl>numw))
{
str1="张三";
str2="李四";
}
if((numz>numl)&&(numw>numl))
{
str1="张三";
str2="王五";
}
if((numw>numz)&&(numl>numz))
{
str1="李四";
str2="王五";
}
}
public void itemStateChanged(ItemEvent e)
{
if(box1.getState()&&box2.getState()&&box3.getState())
{
Checkbox box=(Checkbox)e.getSource();
box.setState(false);
}
}
}
class WindowFrame extends Frame implements WindowListener
{
MyPanel1 panel1;
WindowFrame(String s)
{
super(s);
panel1=new MyPanel1();
panel1.setVisible(true);
add(panel1);
addWindowListener(this);
setBounds(100,200,400,300);
setVisible(true);
validate();
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowOpened(WindowEvent e)
{
}
}
class VoteMoniter extends Frame implements ActionListener
{
Checkbox box1,box2;
CheckboxGroup cbg;
TextArea ta;
Button bt1,bt2,bt3;
int x,y=0;
VoteMoniter(String s)
{
super(s);
setLayout(null);
cbg=new CheckboxGroup();
box1=new Checkbox(" ",true,cbg);
box2=new Checkbox(" ",false,cbg);
ta=new TextArea();
box1.setBounds(150,45,40,15);
box2.setBounds(200,45,40,15);
ta.setBounds(50,70,300,175);
bt1=new Button("投票");
bt2=new Button("统计");
bt3=new Button("返回");
bt1.setBounds(115,250,46,20);
bt2.setBounds(166,250,46,20);
bt3.setBounds(217,250,46,20);
add(box1);
add(box2);
add(ta);
add(bt1);
bt1.addActionListener(this);
add(bt2);
bt2.addActionListener(this);
add(bt3);
bt3.addActionListener(this);
setBounds(100,200,400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt3)
{
setVisible(false);
Exec6.wf.setVisible(true);
}
if(e.getSource()==bt1)
{
if(box1.getState())
x++;
if(box2.getState())
y++;
}
if(e.getSource()==bt2)
{
ta.setText("");
ta.append("\n两人的竞选票数如下"+"\n\n");
ta.append(box1.getLabel()+"的票数为:"+x+"\n\n");
ta.append(box2.getLabel()+"的票数为:"+y+"\n\n");
ta.append("总的票数为:"+(x+y)+"\n\n");
}
}
}
public class Exec6
{
static WindowFrame wf;
static VoteMoniter vm;
public static void main(String args[])
{
wf=new WindowFrame("简单的投票系统");
vm=new VoteMoniter("简单的投票系统");
}
}