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

The squence of the windows above most show when actions 1, 2, 3, and 4 listed be

ID: 3627571 • Letter: T

Question

The squence of the windows above most show when actions 1, 2, 3, and 4 listed below.

Write a complete program named Buttons that implements a window with three buttons – left, blue, and reset. When the left button is clicked, the window’s contents (the three buttons) shifts left. When the blue button is clicked, the window’s background color changes to blue. When the reset button is clicked, the window’s contents shifts back to the original center alignment and the window’s background color changes back to its original color.

In implementing the reset functionality, don’t try to figure out a color constant that’s used for the window’s original color value. That’s counterproductive because the color might be different on different computers. Instead, retrieve and save the window’s original color and then when the color needs to be reset, use that saved original color value.

1. Original display:
2. After Left button is clicked:
3. After Blue button is clicked:
4. After Reset button is clicked:


As always:

• Limit your use of class variables and instance variables – only use them if appropriate.

• Use appropriate modifiers for your methods. The modifiers we’ve discussed are private, public, protected, static, and final.

• Use helping methods if appropriate.

• Mimic the sample session precisely. In particular, note the window’s title, the window’s size, and the positions of components.

Explanation / Answer

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

public class Buttons extends JFrame {
    //Control Definitions
    JButton resetButton;
    JButton leftButton;
    JButton colorButton;
    JPanel buttonPanel;
    // Layout Definiton
    eventHandle evt;
    FlowLayout flt;
    Point point; //to Hold Previous Window Position
    Color color; //to Hold Previous Color

    public Buttons() {
        super("Buttons Window");
        flt = new FlowLayout();//inialize the Flow Layout
        buttonPanel = new JPanel(flt);//inialize the buttonPanel With Flow Layout
        //initialize buttons
        resetButton = new JButton("Reset");
        leftButton = new JButton("Left");
        colorButton = new JButton("Blue");
        evt = new eventHandle(); //initiate the eventhandle class
        buttonPanel.add(leftButton); //add leftButton
        buttonPanel.add(colorButton);//add colorButton
        buttonPanel.add(resetButton);//add colorButton
        getContentPane().add(buttonPanel);//buttonPanel

        //add actionlistners
        leftButton.addActionListener(evt);
        colorButton.addActionListener(evt);
        resetButton.addActionListener(evt);
        setBounds(20, 120, 250, 70);
       
        //following Initate the point with Center of Scren
        point=new Point((Toolkit.getDefaultToolkit().getScreenSize().width- getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height-getHeight())/2);
      
        setLocation(point); //locates the window in center


        color = buttonPanel.getBackground();//stores the initial color
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


    }

    class eventHandle implements ActionListener { //Event Handler

        public void actionPerformed(ActionEvent e) {
            {
                if (e.getSource() == leftButton) ///if its from leftButton
                {
                    setLocation( (point.x +150), point.y);//shift the window 150 pixels left

                } else if (e.getSource() == colorButton) {
                    buttonPanel.setBackground(color.BLUE); //sets the backgorund to Blue


                } else {

                    setLocation(point);  //sets the location to previous location
                    buttonPanel.setBackground(color); //sets the background to previous color
                }

            }
        }
    }

    public static void main(String[] args) {
        Buttons btnwindow = new Buttons();

    }
}

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