⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 electionframe.java

📁 一个用JAVA 写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class ElectionFrame extends JFrame implements ActionListener
{
    // instance variables
    ChartFrame aChartFrame;
    
    private CandidateList candidateList ;
    private BallotPapers ballotPapers;
    private String candidateId ;     
    private String candidateName ;
	 private int firstVotesNumber;
    private int otherVotesNumber;
	 private int curRound ;
	 private boolean elected;
	 private String showStr ;
								   
    // GUI components
    private JMenuItem exitMenuItem;
    private JButton electionRoundButton, displayBarChartButton, closeButton,
    						findButton;
    private JLabel candidateIdLabel, candidateNameLabel,voteNumberLabel, 
    						otherVotesNumberLabel,roundLabel,electedLabel;
    private JTextField candidateIdTextField,candidateNameTextField, 
   			 				voteNumberTextField, otherVotesNumberTextField,roundTextField;
	 private JCheckBox electedCheckBox;
    private JPanel North, middle, South;
    private JTextArea displayArea;
    private Container pane;
    
    // basic layout and initialisation
    public ElectionFrame()
    {
    	  candidateList = new CandidateList();	
        ballotPapers = new BallotPapers();
        candidateId = "";     
	     candidateName = "";
		  firstVotesNumber = 0;
	     otherVotesNumber = 0;
	     curRound = 0;
	     elected = false;
	     showStr = "";
	     
        setSize(800, 400);
        setTitle("Election");
        setDefaultCloseOperation( EXIT_ON_CLOSE);
        pane = getContentPane();        
        layoutGUI();
		  initCondidateList();
		  initBallotPapers();        
    }
    
    // lay out GUI
    public void layoutGUI()
    {
       createMenu();
       layoutNorthPanel();
       layoutMiddlePanel();
       layoutSouthPanel();       
    }
    
    // create a menu with exit menu item
    public void createMenu()
    {
       JMenuBar menubar = new JMenuBar();
       setJMenuBar(menubar);
       
       JMenu fileMenu 	= new JMenu("File");
       menubar.add(fileMenu);
       
       exitMenuItem 		= new JMenuItem("Exit");
       fileMenu.add(exitMenuItem);
       exitMenuItem.addActionListener(this);
    }
    
    // lay out North panel
    public void layoutNorthPanel()
    {
       	North = new JPanel();
        
        electionRoundButton 	= new JButton("Election Round");
        electionRoundButton.addActionListener(this);
        North.add(electionRoundButton);
        
        displayBarChartButton = new JButton("Display Bar Chart");
        displayBarChartButton.addActionListener(this);
        North.add(displayBarChartButton);
        
        closeButton 				= new JButton("Close");
        closeButton.addActionListener(this);
        North.add(closeButton);
        
        pane.add(North, "North");
    }
    
    // layout middle panel, add sales records to it
    public void layoutMiddlePanel()
    {                     
       displayArea = new JTextArea(10,48);
       displayArea.setFont(new Font("Monospaced",Font.PLAIN,14));
       JScrollPane dispSPane = new JScrollPane(displayArea);
       pane.add(dispSPane,"Center");
    }
    
    // layout South panel
    public void layoutSouthPanel()
    {
        South = new JPanel();
            // layout SouthLeft panel
        JPanel SouthLeft 		= new JPanel();
        SouthLeft.setLayout(new GridLayout(3,1));
        
        candidateIdLabel 		= new JLabel("Find details for this candidate (Id):");
        SouthLeft.add(candidateIdLabel);
        
        candidateIdTextField 	= new JTextField(10);
        SouthLeft.add(candidateIdTextField);
        
        findButton 				= new JButton("Find");
        findButton.addActionListener(this);
        SouthLeft.add(findButton);
      
           // layout SouthRight panel
        JPanel SouthRight 		= new JPanel();
        SouthRight.setLayout(new GridLayout (5, 2));
        
        candidateNameLabel 	= new JLabel("Name:");
        SouthRight.add(candidateNameLabel);
        
        candidateNameTextField = new JTextField(8);
        SouthRight.add(candidateNameTextField);
        
        voteNumberLabel				= new JLabel("Votes gained in first round:");
        SouthRight.add(voteNumberLabel);
        
        voteNumberTextField 		= new JTextField(8);
        SouthRight.add(voteNumberTextField);
        
        otherVotesNumberLabel 	= new JLabel("Other votes gained:");
        SouthRight.add(otherVotesNumberLabel);
        
        otherVotesNumberTextField = new JTextField(8);
        SouthRight.add(otherVotesNumberTextField);
        
        roundLabel 					= new JLabel("Eliminated in round:");
        SouthRight.add(roundLabel);
        
        roundTextField 				= new JTextField(8);
        SouthRight.add(roundTextField);
        
        electedLabel					= new JLabel("Elected");
        SouthRight.add(electedLabel);
        
        electedCheckBox				= new JCheckBox();
        SouthRight.add(electedCheckBox);

        
        // add SouthLeft and SouthRight panels to South panel then add South to pane
        South.add(SouthLeft, "Left");
        JLabel blankLabel = new JLabel("       ");
        South.add(blankLabel);
        South.add(SouthRight, "Right");
        
        pane.add(South, "South");
    }
    
    // initialise card list from file
    public void initCondidateList()
    {       
        try
        {  
            FileReader 			fr 			= new FileReader("CandidatesIn.txt");
            BufferedReader 	br 			= new BufferedReader(fr); 
            boolean 				endOfFile 	= false;

            while (!endOfFile)            
            {
                String line = br.readLine();
                if (line == null)
                    endOfFile = true;
                else
                {
                		StringTokenizer st = new StringTokenizer(line);
		               String id;
		               String name = "";
                	  	id 	= st.nextToken();
		               while (st.hasMoreTokens()) 
		               {
		               	name 	+= " "+ st.nextToken();   
			            }
                    	Candidate candidate = new Candidate(id,name);
                    	candidateList.add(candidate);
                }
            }                                
            
            br.close();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
            System.exit(1);            
        }
    }//initCondidateList end
     
    // responses to button clicking
    public void actionPerformed(ActionEvent ae)
    {
        Object source = ae.getSource();
        if (source == exitMenuItem)
            System.exit(0);
        if (source == electionRoundButton)
            doElect();
        if (source == displayBarChartButton)
            doBarChart();
        if (source == closeButton)                  
            doClose();                                 
        if (source == findButton)
        {
        		Candidate aCandidate = new  Candidate(); 
            String tmp = "";
            String id;
            
            if (candidateIdTextField.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Candidate Id missing!");
                return;
            }
            else
            {
                id = candidateIdTextField.getText().trim();
        			 aCandidate = candidateList.find(id);    // find Candidate
        			 if (aCandidate  == null)    // if the Candidate not exists
        				{
            			JOptionPane.showMessageDialog(null, "Candidate does not exist");
            			return ;
       				 }
       			 else
        				{
        					candidateNameTextField.setText(aCandidate.getName());
					      tmp = "" + aCandidate.getVotesNumber();
		      			voteNumberTextField.setText(tmp); 
					      tmp = "" + aCandidate.getOtherVotesNumber();
		      			otherVotesNumberTextField.setText(tmp);
					      tmp = "" + aCandidate.getEliminatedRound();
		      			roundTextField.setText(tmp);
		      			electedCheckBox.setSelected(aCandidate.isElected());
 			         } 
				}              
        }

    } //actionPerformed() end   
    
    public void initBallotPapers()
    {
    	  boolean endOfFile = false;


        try
        {  
            FileReader 				fr 			= new FileReader("BallotsIn.txt");
        		BufferedReader 		br 			= new BufferedReader(fr); 
            while (!endOfFile)            
            {
                String line = br.readLine();
                if (line == null)
                {
                    endOfFile = true;
                }
                else
                {
                		StringTokenizer st = new StringTokenizer(line);
		               String id;
		               Ballot ballot = new Ballot();
		               while (st.hasMoreTokens()) 
		               {
		               			id = st.nextToken();   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -