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(tr ue);
}
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.WHIT E);
buttonPane1.setLayout(new FlowLayout());
JButton stopButton = new JButton("Red");
stopButton.setBackground(C olor.RED);
stopButton.addActionListen er(this);
buttonPane1.add(stopButton );
JButton goButton = new JButton("Green");
goButton.setBackground(Col or.GREEN);
goButton.addActionListener (this);
buttonPane1.add(goButton);
JButton changeButton = new JButton("Change");
goButton.setBackground(Col or.GREEN);
changeButton.addActionList ener(this) ;
buttonPane1.add(changeButt on);
contentPane.add(buttonPane 1, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEven t e)
{
Container contentPane = getContentPane();
if (e.getActionCommand().equa ls("Red"))
{
contentPane.setBackground( Color.RED) ;
buttonPane1.setBackground( Color.PINK );
state = 1;
}
else if (e.getActionCommand().equa ls("Green" ))
{
contentPane.setBackground( Color.GREE N);
buttonPane1.setBackground( Color.BLUE );
state = -1;
}
else if (e.getActionCommand().equa ls("Change "))
{
if(state==1)
{
buttonPane1.setBackground( Color.RED) ;
state=2;
}
else if(state==-1)
{
buttonPane1.setBackground( Color.GREE N);
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.");
}
}
--------------------------
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(tr
}
public FantasyFootball()
{
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Your Football Team's Official Colors");
Container contentPane = getContentPane();
contentPane.setBackground(
contentPane.setLayout(new BorderLayout());
contentPane.add (new JLabel("Here Are Your Team's Colors", SwingConstants.CENTER));
buttonPane1 = new JPanel();
buttonPane1.setBackground(
buttonPane1.setLayout(new FlowLayout());
JButton stopButton = new JButton("Red");
stopButton.setBackground(C
stopButton.addActionListen
buttonPane1.add(stopButton
JButton goButton = new JButton("Green");
goButton.setBackground(Col
goButton.addActionListener
buttonPane1.add(goButton);
JButton changeButton = new JButton("Change");
goButton.setBackground(Col
changeButton.addActionList
buttonPane1.add(changeButt
contentPane.add(buttonPane
}
public void actionPerformed(ActionEven
{
Container contentPane = getContentPane();
if (e.getActionCommand().equa
{
contentPane.setBackground(
buttonPane1.setBackground(
state = 1;
}
else if (e.getActionCommand().equa
{
contentPane.setBackground(
buttonPane1.setBackground(
state = -1;
}
else if (e.getActionCommand().equa
{
if(state==1)
{
buttonPane1.setBackground(
state=2;
}
else if(state==-1)
{
buttonPane1.setBackground(
state=-2;
}
else if(state==2)
{
buttonPane1.setBackground(
state=1;
}
else if(state==-2)
{
buttonPane1.setBackground(
state=-1;
}
}
else
System.out.println("Error in button interface.");
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.