Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*************Complete the following code in Java to create a GUI. Write code whe

ID: 641752 • Letter: #

Question

*************Complete the following code in Java to create a GUI. Write code where there is //To Do comments at.*************

import javax.swing.*;

public class BowlingGameViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new BowlingGameFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
   A BowlingGame has a score.
*/
public class BowlingGame implements Comparable
{

   /**
      Constructs a bowling game with a given score.
      @param initialBalance the initial score
   */
   public BowlingGame(int initialScore)
   {
      score = initialScore;
   }

  
   /**
      Gets the score of the bowling game
      @return the score
   */
   public int getScore()
   {
      return score;
   }

// TODO: multiple imports required here for ArrayList, Collections, Events and GUI components

public class BowlingGameFrame extends JFrame
{
// Instance variables
   private ArrayList<BowlingGame> games;

// GUI components as instance fields
// TODO: Add rest of GUI components here
   private JPanel panel;

   private static final int FRAME_WIDTH = 400;
   private static final int FRAME_HEIGHT = 250;


   public BowlingGameFrame()
   {
      games = new ArrayList<BowlingGame>();

     // Populate with 10 scores
     games.add(new BowlingGame(150));
     games.add(new BowlingGame(250));
     games.add(new BowlingGame(125));
     games.add(new BowlingGame(166));
     games.add(new BowlingGame(133));
     games.add(new BowlingGame(202));
     games.add(new BowlingGame(188));
     games.add(new BowlingGame(177));
     games.add(new BowlingGame(144));
     games.add(new BowlingGame(199));
     Collections.sort(games);
   
// TODO: Initialize GUI instance fields
   
   
      // Use helper methods
      createButton();
      createPanel();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   private void createButton()
   {
// TODO: create button, create listener, attach listener to button

   }

   private void createPanel()
   {
      panel = new JPanel();
// TODO: add GUI components to panel

      add(panel);
   }

}

   public int compareTo(Object otherObject)
   {
    BowlingGame otherGame = (BowlingGame) otherObject;
    return score - otherGame.getScore();
   }

// TODO: Override toString method - display name of class and instance fields in square brackets


   private int score;
}

Explanation / Answer

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BowlingGameViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new BowlingGameFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      System.out.println(frame);
   }
}

/**
   A BowlingGame has a score.
*/
class BowlingGame implements Comparable
{

   private int score;


/**
      Constructs a bowling game with a given score.
      @param initialBalance the initial score
   */
   public BowlingGame(int initialScore)
   {
      score = initialScore;
   }


   /**
      Gets the score of the bowling game
      @return the score
   */
   public int getScore()
   {
      return score;
   }


@Override
public int compareTo(Object arg0) {
   return 0;
}

// TODO: multiple imports required here for ArrayList, Collections, Events and GUI components

}
class BowlingGameFrame extends JFrame
{
// Instance variables
   private ArrayList<BowlingGame> games;

// GUI components as instance fields
// TODO: Add rest of GUI components here
   private JPanel panel;
   private JButton button;

   private static final int FRAME_WIDTH = 400;
   private static final int FRAME_HEIGHT = 250;


   public BowlingGameFrame()
   {
      games = new ArrayList<BowlingGame>();

     // Populate with 10 scores
     games.add(new BowlingGame(150));
     games.add(new BowlingGame(250));
     games.add(new BowlingGame(125));
     games.add(new BowlingGame(166));
     games.add(new BowlingGame(133));
     games.add(new BowlingGame(202));
     games.add(new BowlingGame(188));
     games.add(new BowlingGame(177));
     games.add(new BowlingGame(144));
     games.add(new BowlingGame(199));
     Collections.sort(games);

// TODO: Initialize GUI instance fields


      // Use helper methods
      createButton();
      createPanel();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   private void createButton()
   {
// TODO: create button, create listener, attach listener to button
button=new JButton("ok");
button.setBounds(50, 50, 100, 50);

button.addActionListener(new ActionListener() {
  
   @Override
   public void actionPerformed(ActionEvent e) {
       // TODO Auto-generated method stub
               panel.setBackground(new Color(222));  
   }
});
   }

   private void createPanel()
   {
      panel = new JPanel();
// TODO: add GUI components to panel
      add(button);
      add(panel);
    
    
   }

   public int compareTo(Object otherObject)
   {
    BowlingGame otherGame = (BowlingGame) otherObject;
    return score - otherGame.getScore();
   }


@Override
public String toString() {
   return "BowlingGameFrame [games=" + games + ", panel=" + panel
           + ", button=" + button + ", score=" + score + "]";
}


// TODO: Override toString method - display name of class and instance fields in square brackets
   private int score;
}