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

Write a program that that displays a dialog box show a message and a randomly ch

ID: 3693891 • Letter: W

Question

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 output below.

LnFnUnit7.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 o Set the background and label text color as shown in sample below o Include a label asking to enter name (see sample) o Include a textfield to get input for name (see sample)

o 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

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 output below.

LnFnUnit7.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 o Set the background and label text color as shown in sample below o Include a label asking to enter name (see sample) o Include a textfield to get input for name (see sample)

o 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

Explanation / Answer

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;

public class SwingColor extends JFrame implements ActionListener
   {
       // Define Components
       JTextField t;
       JButton b;
       JLabel l2;
      
       void ColorSwing()
           {
               setSize(300,300);
               setLayout(new FlowLayout());
               setVisible(true);
              
               Color color[] = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.WHITE}; // define a color array to get the required color basing on index value generated randomly

               // Random number code

               Random r = new Random();
               int col = r.nextInt((4 - 0) + 1) + 0;
              
                              
               getContentPane().setBackground(color[col]); // Set color to background
              
               JLabel l = new JLabel("Enter Your Name");
               l2 = new JLabel();
               t = new JTextField(10);
               b = new JButton("ENTER");
               b.addActionListener(this);
              
               add(l);
               add(t);
               add(b);
               add(l2);
              
               setDefaultCloseOperation(EXIT_ON_CLOSE);
           }
          
           public void actionPerformed(ActionEvent e)
               {
                   if(e.getSource() == b)
                       {
                           l2.setText("Thank you for playing " + t.getText());
                       }
               }  
          
           public void createGUI()
               {
                  
               int inp = JOptionPane.showOptionDialog(null, "Hello World", "The title", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);

               if(inp == JOptionPane.OK_OPTION)
                   {                  
                       ColorSwing();
                   }
           }
      
       public static void main(String args[])
           {
               SwingColor s = new SwingColor();
               s.createGUI();
           }
   }
              
              

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