Reputation: 3
I'm trying write a program where I have 3 different buttons and when you click a button it changes the background of a panel in the frame. I got my panel set up and everything is in the right place, but I need to have all my actionlisteners for my buttons all in one class. I tried doing that but all the buttons change colors and not the background. Here's the code I have so far.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Kelle Schmitt
*/
public class BackgroundColorChooserFrame extends JFrame
{
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
private JLabel titleLbl;
private JButton redBtn;
private JButton greenBtn;
private JButton blueBtn;
private JButton quitBtn;
public BackgroundColorChooserFrame()
{
createButton();
createLabel();
createPanel();
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
redBtn.setBackground(Color.red);
greenBtn.setBackground(Color.green);
blueBtn.setBackground(Color.blue);
}
}
public void createButton()
{
redBtn = new JButton("Red");
greenBtn = new JButton("Green");
blueBtn = new JButton("Blue");
ActionListener colorlistener = new ColorListener();
redBtn.addActionListener(colorlistener);
greenBtn.addActionListener(colorlistener);
blueBtn.addActionListener(colorlistener);
}
public void createLabel()
{
titleLbl = new JLabel("Background Color Chooser");
}
//create and add panels
public void createPanel()
{
JPanel mainPnl, titlePnl, colorPnl, controlPnl;
mainPnl = new JPanel();
mainPnl.setLayout(new BorderLayout());
titlePnl = new JPanel();
colorPnl = new JPanel();
controlPnl = new JPanel();
mainPnl.add(titlePnl, BorderLayout.NORTH);
titlePnl.add(titleLbl);
mainPnl.add(colorPnl, BorderLayout.CENTER);
mainPnl.add(controlPnl, BorderLayout.SOUTH);
controlPnl.add(redBtn);
controlPnl.add(greenBtn);
controlPnl.add(blueBtn);
//add the mainPnl to the parent frame
add(mainPnl);
}
}
Please help! Thanks!
Upvotes: 0
Views: 326
Reputation: 324088
Maybe something like:
public void actionPerformed(ActionEvent evt)
{
JButton button = (JButton)evt.getSource();
Component parent = button.getParent();
if (button == redBtn)
parent.setBackground( Color.RED );
else if (...)
}
Although a much better solution is to create a separate ActionListener for every button so you don't use nested if/else logic:
public class ColorListener implements ActionListener
{
private Color background;
public ButtonListener(Color background)
{
this.background = background;
}
@Override
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)evt.getSource();
Component parent = button.getParent();
parent.setBackground( background );
}
}
Then you can create an unlimited number of buttons and colors:
redButton.addActionListener( new ColorListener(Color.RED) );
The key point is in using the getSource()
method so you can write generic code.
Upvotes: 3