Question: //////////////////Java Programming help//////////////////////////// Write a program that that displays a dialog box show a message and a randomly chosen color. This random color is to
//////////////////Java Programming help////////////////////////////
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.
//////////////////////////////////////////////////////////////////////////////////////////
Include
*JFrame Class *Java Components *JLabel Components *Component Listeners *Inner Classes *JButton Components *Dialog Boxes and JOptionPane Class *Color
///////////////////////////////////////////////////////////////////////////////////////
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 Output



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I have a code I put together but it has alot of error and I am confused on how to go from here.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random;
public class LiFiUnit7Ch17 extends JFrame{ private static final int WIDTH = 300; // width private static final int HEIGHT = 200; // height JLabel label; // label to ask for name JTextField textField; // textbox for prompt public LiFiUnit7Ch17(Color color, Color fontColors){ setLayout(null); label = new JLabel("What is your name: "); // prompt Container con = getContentPane(); setDefaultCloseOperation(EXIT_ON_CLOSE); con.setBackground(color); con.add(label); label.setForeground(labelColors); textField = new JTextField(15); // text box con.add(textField); textField.addActionListener(new ButtonListener()); setSize(WIDTH, HEIGHT); setVisible(true); } public static void main(String[] args) { Random r = new Random(); int rand = r.nextInt(5); // chosen at random 0 - 4 if (r==0) // random selector for 0 - 4 of colors { setBackground(Color.GREEN); labelColors.setForeground(Color.BLUE); } else if (r==1) { setBackground(Color.RED); labelColors.setForeground(Color.WHITE); } else if (r==2) { setBackground(Color.LIGHT_GRAY); labelColors.setForeground(Color.BLACK); } else if (r==3) { setBackground(Color.BLUE); labelColors.setForeground(Color.WHITE); } else if (r==4) { setBackground(Color.YELLOW); labelColors.setForeground(Color.BLACK); } // end ifs JOptionPane.showMessageDialog(null, "The following window color will be randomly chosen from Red, White, Yellow, Green, Red Your color will be "+rand); // give random color }
private class ButtonListener implements ActionListener{ // inner class for event handling public void ActionPerformed(ActionEvent e) { { String message; // personalized message message ="Thanks for playing "+textField.getText(); // gets the name entered textField.setText(""); label.setText(message); } // end action performed }
} } // end class
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Comments are appreciated as well. Also I have to implement the Information Icon in the first pic but am unsure where to put it so implementing it would be nice.
Message i) The following window color will be randomly chosen from Red, White, Yellow, Green, Blue Your color will be: GREEN OK
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
