Basic Requirements JAVA Write a program that that displays a dialog box show a m
ID: 3768847 • Letter: B
Question
Basic Requirements JAVA
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.
Basic Requirements JAVA
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
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class RandomColors extends Frame implements ActionListener
{
public RandomColors()
{
setLayout(new FlowLayout());
Button btn = new Button("Change Background");
btn.addActionListener(this);
add(btn);
setTitle("Generating Random Colors");
setSize(300, 300);
setVisible(true);
}
public class NamePrompt extends JFrame{
private static final long serialVersionUID = 1L;
String name;
public NamePrompt(){
setLayout(new BorderLayout());
JLabel enterYourName = new JLabel("Enter Your Name Here:");
JTextField textBoxToEnterName = new JTextField();
textBoxToEnterName.setSize(40, 10);
JPanel panelTop = new JPanel();
panelTop.add(enterYourName);
panelTop.add(textBoxToEnterName);
JButton submit = new JButton("Submit");
submit.addActionListener(new SubmitButton());
JPanel panelBottom = new JPanel();
panelBottom.add(submit);
//Add panelTop to JFrame
add(panelTop, BorderLayout.NORTH);
add(panelBottom, BorderLayout.SOUTH);
//JFrame set-up
setTitle("Name Prompt Program");
setSize(300, 150);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e)
{
Random rand = new Random();
int redValue = rand.nextInt(255);
int greenValue = rand.nextInt(255);
int blueValue = rand.nextInt(255);
System.out.println("Red: " + redValue +", Green: " + greenValue + ", Blue: " + blueValue);
Color clr = new Color(redValue, greenValue, blueValue);
setBackground(clr);
}
public static void main(String args[])
{
new RandomColors();
NamePrompt promptForName = new NamePrompt();
promptForName.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.