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

Urgent code fix needed! Answered Code throws error. Directions //PLEASE INCLUDE

ID: 3572115 • Letter: U

Question

Urgent code fix needed! Answered Code throws error.

Directions
//PLEASE INCLUDE COMMENTS!!!

//PLEASE MAKE SURE CODE WORKS!!!

The files must be called <LiFiUnit7Ch17.java>  
The files must be called as specified above, (LiFi = Your Last Initial Your First Initial)
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
Only submit the .java files needed to make the program run.

Style Components
Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892.

Topics to cover
Topics with * need to definitely be covered in this code:
          Event-Driver Programming Basics
          *JFrame Class
          *Java Components
          *JLabel Components
          JTextField Components
          *Component Listeners
          *Inner Classes
          *JButton Components
          *Dialog Boxes and JOptionPane Class
          Distinguising Between Multiple Events
          Using getActionCommand to Distringuish Between Multiple Events
          *Color
          How GUID Classes Are Group Together
          Mouse Listener and Images (Optional)

Basic Requirements
Write a program that that displays a dialog box show a message and a randomly chosen color.

This random color is to be used as the background color of a JFrame window which should appear after “OK” is selected. The window should ask your name and thank you for playing once “Enter” is pressed.

See sample 001-005 output below.

LiFiUnit7Ch17.java

     - Utilize showMessageDialog to output the message shown in the sample at the bottom. A random color should be output with each run
     - Upon clicking OK
     - Create a JFrame window
     - Set the background and label text color as shown in sample below
     - Include a label asking to enter name (see sample)
     - Include a textfield to get input for name (see sample)
     - Upon hitting “Enter” on the keyboard, output thank you message to include the name entered as per sample at bottom
     - Use an inner class for the listener
     - Mimic the same session precisely. Pay attention to colors of the window and label.

Sample
Your output will vary based on the random color generated.
     - Initial dialog box (sample001)
     - Green window with Blue for label color (Sample002)
     - Red window with White for label color (sample003)
     - White(ish) window with Black for label color (sample004)
     - Blue window with White for label color (sample005)

Sample001

Sample002

Sample 003

Sample004

Sample005

(no image, but I am sure you can guess the same but with a Blue Window:)

/////////////////////////////////////////////////////////////////////

Expert Answer had errors contained within the code and did not execute. Can you fix it and add comment?

LiFiUnit7Ch17.java


import java.awt.Color;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class LiFiUnit7Ch17 extends JFrame{

  
   JLabel label;
   JTextField textField;
   public LiFiUnit7Ch17(Color color, Color labelColors){
       setLayout(null);
       Container c = getContentPane();
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       c.setBackground(color);
          
       label = new JLabel("What is your name: ");
       c.add(label);
       label.setForeground(labelColors);
       textField = new JTextField(50);
       c.add(textField);      
       label.setBounds(100, 0, 150, 20);
       textField.setBounds(220, 0, 100, 20);
       textField.addKeyListener(new ButtonListener());
   setSize(400, 200);
   setVisible(true);         
   }  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Color colors[] = {Color.BLUE, Color.WHITE,Color.YELLOW,Color.GREEN,Color.RED};
       String colorStr[] = {"BLUE", "WHITE", "YELLOW", "GREEN", "RED"};
       Color colorLabels[] = {Color.WHITE, Color.BLACK, Color.BLACK, Color.BLUE, Color.WHITE};
       Random r = new Random();
   int rand = r.nextInt(5);
  
       JOptionPane.showMessageDialog(null, "The following window color will be randomly chosen from Red, Whte, Yellow, Green, Red Your color will be "+colorStr[rand]);
   new LiFiUnit7Ch17(colors[rand], colorLabels[rand]);
  
   }


private class ButtonListener implements KeyListener{
   public void keyPressed(KeyEvent e) {
   if (e.getKeyCode()==KeyEvent.VK_ENTER){
       textField.setVisible(false);
       label.setBounds(100, 0, 200, 20);
       label.setText("Thanks for playing "+textField.getText());
      
   }
}

   public void keyReleased(KeyEvent e) {
       // TODO Auto-generated method stub
      
   }

   public void keyTyped(KeyEvent e) {
       // TODO Auto-generated method stub
      
   }
}

Message i The following window color will be randomly chosen from Red, White, Yellow, Green, Blue Your color will be: GREEN OK

Explanation / Answer

Hey,

Just add a curly brace( "}") at the end of the program.See below code


import java.awt.Color;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class LiFiUnit7Ch17 extends JFrame{
  
JLabel label;
JTextField textField;
public LiFiUnit7Ch17(Color color, Color labelColors){
setLayout(null);
Container c = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setBackground(color);
  
label = new JLabel("What is your name: ");
c.add(label);
label.setForeground(labelColors);
textField = new JTextField(50);
c.add(textField);
label.setBounds(100, 0, 150, 20);
textField.setBounds(220, 0, 100, 20);
textField.addKeyListener(new ButtonListener());
setSize(400, 200);
setVisible(true);   
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Color colors[] = {Color.BLUE, Color.WHITE,Color.YELLOW,Color.GREEN,Color.RED};
String colorStr[] = {"BLUE", "WHITE", "YELLOW", "GREEN", "RED"};
Color colorLabels[] = {Color.WHITE, Color.BLACK, Color.BLACK, Color.BLUE, Color.WHITE};
Random r = new Random();
int rand = r.nextInt(5);
  
JOptionPane.showMessageDialog(null, "The following window color will be randomly chosen from Red, Whte, Yellow, Green, Red Your color will be "+colorStr[rand]);
new LiFiUnit7Ch17(colors[rand], colorLabels[rand]);
  
}

private class ButtonListener implements KeyListener{
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
textField.setVisible(false);
label.setBounds(100, 0, 200, 20);
label.setText("Thanks for playing "+textField.getText());
  
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
  
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
  
}
}
} //just curly brace here to complete the LiFiUnit7Ch17 class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote