Link to home
Start Free TrialLog in
Avatar of computerese
computerese

asked on

MORE HELP WITH JAVA BUTTONS AND PANELS

Last week Experts Exchange (CEHJ, Webstorm, et. al.) helped me design a GUI for my Java Fantasy Football game's buttons and panels. However, I'm having a new problem with the code I wrote over the weekend, and I am hoping someone could help me out. I added a "Change" button to my panel. When this button is clicked, the button panel colors should change colors again (for example, from pink [the color of the  button panel] and red [the color of the larger panel] to blue [the color of the button panel] and green [the color of the larger panel], respectively, or vice versa). Can someone help me with this? What am I doing wrong?

---------------------------------------HERE IS MY SOURCE CODE---------------------------------

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

public class FantasyFootball extends JFrame implements ActionListener
{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;
   
    private JPanel buttonPane1;
    private int state;
   
    public static void main(String[] args)
    {
       FantasyFootball guiWithPanel = new FantasyFootball();
        guiWithPanel.setVisible(true);
    }
       
    public FantasyFootball()
    {
        setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer());
        setTitle("Your Football Team's Official Colors");
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.BLUE);
        contentPane.setLayout(new BorderLayout());
       
       
        contentPane.add (new JLabel("Here Are Your Team's Colors", SwingConstants.CENTER));
       
        buttonPane1 = new JPanel();
        buttonPane1.setBackground(Color.WHITE);
       
        buttonPane1.setLayout(new FlowLayout());
       
        JButton stopButton = new JButton("Red");
        stopButton.setBackground(Color.RED);
        stopButton.addActionListener(this);
        buttonPane1.add(stopButton);
       
        JButton goButton = new JButton("Green");
        goButton.setBackground(Color.GREEN);
        goButton.addActionListener(this);
        buttonPane1.add(goButton);
       
        JButton changeButton = new JButton("Change");
        goButton.setBackground(Color.GREEN);
        changeButton.addActionListener(this);
        buttonPane1.add(changeButton);
       
        contentPane.add(buttonPane1, BorderLayout.SOUTH);
    }
    public void actionPerformed(ActionEvent e)
    {
        Container contentPane = getContentPane();
       
        if (e.getActionCommand().equals("Red"))
                {
                contentPane.setBackground(Color.RED);
                buttonPane1.setBackground(Color.PINK);
                state = 1;
                }
        else if  (e.getActionCommand().equals("Green"))
                {
                contentPane.setBackground(Color.GREEN);
                buttonPane1.setBackground(Color.BLUE);
                state = -1;
                }
        else if  (e.getActionCommand().equals("Change"))
                {
                if(state==1)
                    {        
                    buttonPane1.setBackground(Color.RED);
                    state=2;
                    }
                else if(state==-1)
                    {  
                    buttonPane1.setBackground(Color.GREEN);
                    state=-2;
                    }
                else if(state==2)
                    {        
                    buttonPane1.setBackground(Color.PINK);
                    state=1;
                    }
                else if(state==-2)
                    {  
                    buttonPane1.setBackground(Color.BLUE);
                    state=-1;
                    }
                }    
        else
                System.out.println("Error in button interface.");
    }
}
   
SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial