Hello again all,
I'm running into another problem here and, as you all have been very helpful in the past, I'm hoping you can point out my error this time. For all of me I just can't spot it!
I'm creating a java swing/GUI program that creates three buttons called: Left, Blue and Reset that are at the top of the panel and have a centered alignment. When the Left button is clicked, the windows contents (the three buttons) should shift left. When the Blue button is clicked, the windows background color should change to blue. When the Reset button is clicked, the windows contents should shift back to the original center alignment and the windows background color should change back to its original color.
I've gotten everything to appear correctly when the window first appears, and the Blue button works correctly. The color change portion of the Reset button works correctly. However, pressing the Left button does....nothing.
Any idea what I'm doing wrong/leaving out here? I would really appreciate it!
I'm running into another problem here and, as you all have been very helpful in the past, I'm hoping you can point out my error this time. For all of me I just can't spot it!
I'm creating a java swing/GUI program that creates three buttons called: Left, Blue and Reset that are at the top of the panel and have a centered alignment. When the Left button is clicked, the windows contents (the three buttons) should shift left. When the Blue button is clicked, the windows background color should change to blue. When the Reset button is clicked, the windows contents should shift back to the original center alignment and the windows background color should change back to its original color.
I've gotten everything to appear correctly when the window first appears, and the Blue button works correctly. The color change portion of the Reset button works correctly. However, pressing the Left button does....nothing.
Any idea what I'm doing wrong/leaving out here? I would really appreciate it!
import javax.swing.*;//imports swing package
import java.awt.*;//imports awt package
import java.awt.event.*;//imports awt package, specifically for ActionListener
//*************************************************************************************************************************************************************************
public class Buttons extends JFrame//start of main class of program
{//opening bracket of Buttons class
private JButton firstButton = new JButton("");//creates a new instance object of JButton class and names it firstButton
private JButton leftButton = new JButton("Left");//creates a new instance object of JButton class and names it leftButton
private JButton blueButton = new JButton("Blue");//creates a new instance object of JButton class and names it blueButton
private JButton resetButton = new JButton("Reset");//creates a new instance object of JButton class and names it resetButton
//*************************************************************************************************************************************************************************
public static void main(String[] args)//main method of Buttons
{//opening bracket of main method
new Buttons();//creates a new instance of the Buttons object
}//closing bracket of main method
//*************************************************************************************************************************************************************************
public Buttons()//constructor to create the window and populate it
{//opening bracket of constructor
setTitle("Buttons Window");//prints the given text in the title bar of the window
setSize(350,200);//sets the size of the window
setLayout(new FlowLayout());//sets the layout of the window to use FlowLayout.
setDefaultCloseOperation(EXIT_ON_CLOSE);//tells the computer to exit the program if/when the user presses the x button in the upper right corner of the window
createContents();//calls the createContents helper method below to populate the main part of the window
setVisible(true);//makes the window visible
}//closing bracket of constructor
//*************************************************************************************************************************************************************************
private void createContents()//helper method to populate the main part of the window
{//opening bracket of the createContenets helper method
add(firstButton);
firstButton.setVisible(false);
add(leftButton);
add(blueButton);
add(resetButton);
final Color defaultBackground = getContentPane().getBackground();
leftButton.addActionListener(//inner class to add a listener that will act if the user presses the Left button
new ActionListener()//creates a new listener interface of the type ActionListener
{//opening bracket of leftButton listener interface
public void actionPerformed(ActionEvent e)//heading to ensure that all fired events will be received properly by the listener
{//opening bracket of this heading
getContentPane().remove(firstButton);
getContentPane().revalidate();
getContentPane().repaint();
}//closing bracket of this heading
}//closing bracket of leftButton listener interface
);//closing parenthesis and semi colon of the inner class listener interface/completion of the line of code the inner class is part of
blueButton.addActionListener(//inner class to add a listener that will act if the user presses the Blue button
new ActionListener()//creates a new listener interface of the type ActionListener
{//opening bracket of blueButton listener interface
public void actionPerformed(ActionEvent e)//heading to ensure that all fired events will be received properly by the listener
{//opening bracket of this heading
getContentPane().setBackground(Color.BLUE);
}//closing bracket of this heading
}//closing bracket of blueButton listener interface
);//closing parenthesis and semi colon of the inner class listener interface/completion of the line of code the inner class is part of
resetButton.addActionListener(//inner class to add a listener that will act if the user presses the Reset Button
new ActionListener()//creates a new listener interface of the type ActionListener
{//opening bracket of resetButton listener interface
public void actionPerformed(ActionEvent e)//heading to ensure that all fired events will be received properly by the listener
{//opening bracket of this heading
getContentPane().removeAll();
add(firstButton);
firstButton.setVisible(false);
add(leftButton);
add(blueButton);
add(resetButton);
getContentPane().setBackground(defaultBackground);
getContentPane().revalidate();
getContentPane().repaint();
}//closing bracket of this heading
}//closing bracket of resetButton listener interface
);//closing parenthesis and semi colon of the inner class listener interface/completion of the line of code the inner class is part of
}//closing bracket of createContents helper method
}//closing bracket of Buttons class