Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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.");
    }
}
   
0
computerese
Asked:
computerese
3 Solutions
 
sciuriwareCommented:
It is no answer to your question, but if I were you I would no longer use the e.getActionCommand()
it is a source of strange errors, because the compiler doesn't see typo's in command names.
A much better approach is:

    public void actionPerformed(ActionEvent e)
    {
        Container contentPane = getContentPane();
        Object o = a.getSource();                     // Work with references!

        if(o == stopButton)                          // Compiler checked!
                {
                contentPane.setBackground(Color.RED);
                buttonPane1.setBackground(Color.PINK);
                state = 1;
                }
        else if(o == goButton)                      // Also
                {
                contentPane.setBackground(Color.GREEN);
                buttonPane1.setBackground(Color.BLUE);
                state = -1;
                }
  ................


;JOOP!
0
 
KantiCommented:
You can take sciuriware's advice and change your code accordingly . For your problem now this might help.

initialze your state to something like

private int state = 2;

Change

JButton changeButton = new JButton("Change");
goButton.setBackground(Color.GREEN);

to

JButton changeButton = new JButton("Change");
changeButton.setBackground(Color.GREEN);


This below actionPerformed code should be enough

 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 = 2;
                }
        else if  (e.getActionCommand().equals("Change"))
                {
                 if(state==1)

                    {        
                  contentPane.setBackground(Color.GREEN);
                     buttonPane1.setBackground(Color.BLUE);
                    state=1 ;
                   
                    }
                else
                    {  
                      contentPane.setBackground(Color.RED);
                     buttonPane1.setBackground(Color.PINK);
                      
                     state=2;
                    }
             
                }    
        else
         System.out.println("Error in button interface.");
    }
0
 
mukundha_expertCommented:
Check out this Code ... This one is working.


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)
                    {        
                      contentPane.setBackground(Color.GREEN);
                    buttonPane1.setBackground(Color.BLUE);
                    state=-1;
                    }
                else if(state==-1)
                    {  
                    contentPane.setBackground(Color.RED);
                    buttonPane1.setBackground(Color.PINK);
                    state=1;
                    }
                }    
        else
                System.out.println("Error in button interface.");
    }
   
   
}
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now