Logout

Full GUI - Using OOP - Example 

Not only is this GUI full, and usefully functional; also, it uses another class from which it makes an array of "Teams" instances, and so it is fully OOP as well.

All of this - both GUI and OOP - will be taught and pracitced during the next phase of the course.

(Recall that GUI stands for Graphical User Interface, and OOP stands for Object Oriented Programming.)

Here is a link to this Round-Robin Sports Tourney application, which you can download and run on your Mac or PC computers as a stand-alone application. (Remember to not change the location of any of the files inside the downloaded zip folder, which you will un-zip. The executable file is the .jar file.)

(And here's the Netbeans file if you want to play around with it at some point.)




CEESATournament.java




CEESATournament.java (uses Team.java - see below)
/Users/adelaide/Public/Netbeans - All JSR Projects/CEESA Tournament/src/ceesatoruney/CEESATournament.java
  1 
  2 /*
  3  * CEESATournament.java
  4  *
  5  * Created on Nov 9, 2010, 6:48:25 PM by John Rayworth

      This program allows the user to input the names of four teams for a round-robin sports tournament in whihc each team plays each other team once.
      This version of the program awards three points for each win, and one for each tie, and keeps track of the points and the standings as the Games are played.

  6  */
  7 
  8 package ceesatoruney;
  9 
 10 
 11 public class CEESATournament extends javax.swing.JFrame {
 12 
 13     private Team team1 = new Team(); /*Refer to the Team class below this class, to see what it is made of and what hppens when a new Team is made (it's primarily that 
         the points attribute is set to 0.*/
 14     private Team team2 = new Team();
 15     private Team team3 = new Team();
 16     private Team team4 = new Team();
 17     private Team [] teamsArray = {team1, team2, team3, team4}; /*Making an array of the above four "Team" objects. 
			 (This has to be done with global scope since it will be accessed by several methods.*/
 20 
 21     public CEESATournament() {
 22         initComponents();
 23     }
 24 
          //Note that all of the auto-generated Netbeans code is not showing here.

        //The method for setting the names of the teams in the teams array, and also for placing the team names for each game in the GUI.
648     private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {                                      
649 
650         team1.setTeamName(jTextFieldTeam1NameInput.getText());
651         team2.setTeamName(jTextFieldTeam2NameInput.getText());
652         team3.setTeamName(jTextFieldTeam3NameInput.getText());
653         team4.setTeamName(jTextFieldTeam4NameInput.getText());
654 
655         jLabelTeam1_1.setText(teamsArray[0].getTeamName());
656         jLabelTeam1_2.setText(teamsArray[0].getTeamName());
657         jLabelTeam1_3.setText(teamsArray[0].getTeamName());
658 
659         jLabelTeam2_1.setText(teamsArray[1].getTeamName());
660         jLabelTeam2_2.setText(teamsArray[1].getTeamName());
661         jLabelTeam2_3.setText(teamsArray[1].getTeamName());
662     
663         jLabelTeam3_1.setText(teamsArray[2].getTeamName());
664         jLabelTeam3_2.setText(teamsArray[2].getTeamName());
665         jLabelTeam3_3.setText(teamsArray[2].getTeamName());
666 
667         jLabelTeam4_1.setText(teamsArray[3].getTeamName());
668         jLabelTeam4_2.setText(teamsArray[3].getTeamName());
669         jLabelTeam4_3.setText(teamsArray[3].getTeamName());
670  
671     } 

 25     private void refreshCurrentStandings(){
 28          /*This method will be called whenever a new game's score is added, which happens whenever one of the six Ok buttons is pressed. 
 31         See the code for those below.*/
 32         teamsArray = bubbleSort1(teamsArray); /*Before we refresh the order of the teams, first through fourth, we need to sort them, so we call the
 33         bubbleSort method.  It sends the unsorted teamsArray, and returns the sorted one.*/
 35          
 37         //Now, one at a time, we refresh the GUI with the top team and it's points.
 38         jTextFieldFirstPlaceTeam.setText(teamsArray[0].getTeamName());
 40         jTextFieldFirstPlacePoints.setText(teamsArray[0].getTeamPoints() +"");
 41         
 42         jTextFieldSecondPlaceTeam.setText(teamsArray[1].getTeamName());
 43         jTextFieldSecondPlacePoints.setText(teamsArray[1].getTeamPoints() +"");
 44         
 45         jTextFieldThirdPlaceTeam.setText(teamsArray[2].getTeamName());
 46         jTextFieldThirdPlacePoints.setText(teamsArray[2].getTeamPoints() +"");
 47         
 48         jTextFieldFourthPlaceTeam.setText(teamsArray[3].getTeamName());
 49         jTextFieldFourthPlacePoints.setText(teamsArray[3].getTeamPoints() +"");
 50 
 51     }
 52 
 53     /*Following is a method that I copies off of the internet which with some modifications does a good job of sorting the teams according to the 
 54      number of points they have.  We'll learn this "bubble sort" in detail in a couple of months.  But for now, copying and pasting and using it does the trick.*/
 58      
 59     public Team[] bubbleSort1(Team[] x) {
 60     /*You can understand that it takes in a parameter, x, which is an array of team objects.  And it also returns an array of Team objects - which will
 61     be sorted after the looping is done.*/
 63     int n = x.length;
 64     for (int pass=1; pass < n; pass++) {  
 65         for (int i=0; i < n-pass; i++) {
 66             if (x[i].getTeamPoints() < x[i+1].getTeamPoints()) {
 67                 /*Don't worry about how this bubble sort works , but I'll mention at this stage that the following three lines swap the elements
 68                one at a time.  But what's most important to note is that what is being swapped is the elements of the Team array x.*/
 70                 Team temp = x[i];  
 72                 x[i] = x[i+1];  
 73                 x[i+1] = temp;
 74             }
 75         }
 76     }
 77     return x;
 78     //The sorted Team array is returned
 79 }
 80 
 81     /*This is the method that will calculate the points, depending on which teams and how many goals they scored are sent.*/
 82     private void calculatePoints(String teamA, String teamB, int goalsA, int goalsB){
 84     /*Do notice that it takes in two strings, for telling which two teams are being dealt with, along with two ints, representing the goals scored by each.*/
 85      
 88         /*If the team on the left has scored more goals than the team on the  right.  That team gets 3 points for a win.*/
 89        if(goalsA > goalsB){
 91            if(teamA.equals("Team 1")){
 92                 team1.increaseTeamPoints(3);
 93            }
 94             else if(teamA.equals("Team 2")){
 95                 team2.increaseTeamPoints(3);
 96            }
 97            else if(teamA.equals("Team 3")){
 98                 team3.increaseTeamPoints(3);
 99            }
100            else if(teamA.equals("Team 4")){
101                 team4.increaseTeamPoints(3);
102            }
103         }
104        /*If the team on the right has scored more goals than the team on the left. That team gets 3 points for a win.*/
106        else if(goalsB > goalsA){
107              if(teamB.equals("Team 1")){
108                 team1.increaseTeamPoints(3);
109            }
110             else if(teamB.equals("Team 2")){
111                 team2.increaseTeamPoints(3);
112            }
113            else if(teamB.equals("Team 3")){
114                 team3.increaseTeamPoints(3);
115            }
116            else if(teamB.equals("Team 4")){
117                 team4.increaseTeamPoints(3);
118            }
119        
120        }
121         else{/*It's a tie. 
122             Note here that 1, not 3 is sent to the increaseTeamPoints method,  since a tame gets 1 point for a tie.*/
123           if(teamA.equals("Team 1")){
125                 team1.increaseTeamPoints(1);
126            }
127             else if(teamA.equals("Team 2")){
128                 team2.increaseTeamPoints(1);
129            }
130            else if(teamA.equals("Team 3")){
131                 team3.increaseTeamPoints(1);
132            }
133            else if(teamA.equals("Team 4")){
134                 team4.increaseTeamPoints(1);
135            }
136            
137            if(teamB.equals("Team 1")){
138                 team1.increaseTeamPoints(1);
139            }
140             else if(teamB.equals("Team 2")){
141                 team2.increaseTeamPoints(1);
142            }
143            else if(teamB.equals("Team 3")){
144                 team3.increaseTeamPoints(1);
145            }
146            else if(teamB.equals("Team 4")){
147                 team4.increaseTeamPoints(1);
148            }
149        }
150     }
151         
617     /*Each time one of the six side buttons is pressed, two things happen: the new points are added with the calculatePoints method, and then the standings are updated
          and refreshed with the refreshCurrentStandings method.*/ 
618     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
619        calculatePoints("Team 1", "Team 2", Integer.parseInt(jTextField_G1_A.getText()), Integer.parseInt(jTextField_G1_B.getText()));
620        refreshCurrentStandings();
621     }                                        
622 
623     private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       
624        calculatePoints("Team 3", "Team 4", Integer.parseInt(jTextField_G2_A.getText()), Integer.parseInt(jTextField_G2_B.getText()));
625        refreshCurrentStandings();
626     }                                      
627 
628     private void jButton5MouseReleased(java.awt.event.MouseEvent evt) {                                       
629        calculatePoints("Team 2", "Team 3", Integer.parseInt(jTextField_G3_A.getText()), Integer.parseInt(jTextField_G3_B.getText()));
630        refreshCurrentStandings();
631     }                                      
632 
633     private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {                                       
634        calculatePoints("Team 1", "Team 4", Integer.parseInt(jTextField_G4_A.getText()), Integer.parseInt(jTextField_G4_B.getText()));
635        refreshCurrentStandings();
636     }                                      
637 
638     private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {                                       
639        calculatePoints("Team 1", "Team 3", Integer.parseInt(jTextField_G5_A.getText()), Integer.parseInt(jTextField_G5_B.getText()));
640        refreshCurrentStandings();
641     }                                      
642 
643     private void jButton6MouseReleased(java.awt.event.MouseEvent evt) {                                       
644        calculatePoints("Team 2", "Team 4", Integer.parseInt(jTextField_G6_A.getText()), Integer.parseInt(jTextField_G6_B.getText()));
645        refreshCurrentStandings();
646     }                                      
647 

689     //This is the main method that runs the GUI app.  This is generated by Netbeans. 
690     public static void main(String args[]) {
691         java.awt.EventQueue.invokeLater(new Runnable() {
692             public void run() {
693                 new CEESATournament().setVisible(true);
694             }
695         });
696     }
697 



Team.java (used by CEESATournament.java - see above.)
/Users/adelaide/Public/Netbeans - All JSR Projects/CEESA Tournament/src/ceesatoruney/Team.java
 1 
 2 package ceesatoruney;
 3 
 4 //What I call a template class.
 5 
 6 public class Team {
 7 
 8     //The "attributes"
 9      private String teamName;
10     private int points;
11 
12     //The "constructor"
13     public Team(){
14         points = 0;
15     }
16 
17     /*The "Set" Methods - one for each attribute,
18     for setting the attributes of a particular Team object.*/
19     public void setTeamName(String teamNameTakenIn){
20         teamName = teamNameTakenIn;
21     }
22     public void setTeamPoints(int pointsTakenIn){
23         points = pointsTakenIn;
24 
25     }
26 
27     /*The "Get" Methods - one for each attribute,
28     for sending back particular information about a particular Team object.*/
29     public String getTeamName(){
30         return teamName;
31     }
32     public int getTeamPoints(){
33         return points;
34     }
35 
36     /*This is the other method, which in this case adds points to a
37     /particular Team object's points count.*/
38     public void increaseTeamPoints(int pointsToIncreas){
39         points+= pointsToIncreas;
40     }
41 
42 }