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

Write a program that displays a window derived from a JFrame. Within the window,

ID: 3620704 • Letter: W

Question

Write a program that displays a window derived from a JFrame. Within the window, do the following:

* The JFrame should have Border layout.
* Within the center of the JFrame, place a JPanel.
* On the JPanel, place two buttons. These should be 100 pixels wide and 50 pixels high, and should always display at that size.
* When a button is pressed, its color should change. The colors should cycle through six different colors, with the sixth button press cycling back to the original color.
* Your JFrame should have a title.
* This program is easiest to write if you derive your own class from JFrame.

Explanation / Answer

import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JComponent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.BorderFactory; public class Colors extends JPanel implements ActionListener { private static boolean PLATFORM = false; int buttonLabelIndex = 0; String buttonLabel[] = {"Red","Blue","Green","Orange", "Pink","Yellow" }; Color buttonColor[] ={Color.RED,Color.BLUE,Color.GREEN,Color.ORANGE,Color.PINK,Color.YELLOW}; JButton Button; public Colors() { super(new BorderLayout()); Button = new JButton(buttonLabel[buttonLabelIndex]); if(PLATFORM ) { Button.setBackground(buttonColor[buttonLabelIndex]); } else { Button.setForeground(buttonColor[buttonLabelIndex]); } Button.addActionListener(this); this.add(Button, BorderLayout.CENTER); this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); } public void actionPerformed(ActionEvent e) { buttonLabelIndex = ++buttonLabelIndex
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