简体   繁体   中英

Using one button to change the color of multiple JPanels

So i have three panels that i have three different buttons for to change them each to their respective colors. I need to add a fourth button that will return all three panels to their original default light gray color. I add this "reset" button and it only changes the first panel back. What am i doing wrong?

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PanelDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JPanel redPanel;
private JPanel whitePanel;
private JPanel bluePanel;

public static void main(String[] args)
{
    PanelDemo gui = new PanelDemo();
    gui.setVisible(true);
}
public PanelDemo()
{
    super("Panel Demonstration");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    JPanel biggerPanel = new JPanel();
    biggerPanel.setLayout(new GridLayout(1, 3));

    redPanel = new JPanel();
    redPanel.setBackground(Color.LIGHT_GRAY);
    biggerPanel.add(redPanel);

    whitePanel = new JPanel();
    whitePanel.setBackground(Color.LIGHT_GRAY);
    biggerPanel.add(whitePanel);

    bluePanel = new JPanel();
    bluePanel.setBackground(Color.LIGHT_GRAY);
    biggerPanel.add(bluePanel);

    add(biggerPanel, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setBackground(Color.LIGHT_GRAY);
    buttonPanel.setLayout(new FlowLayout());

    JButton redButton = new JButton("Red");
    redButton.setBackground(Color.RED);
    redButton.addActionListener(this);
    buttonPanel.add(redButton);

    JButton whiteButton = new JButton("White");
    whiteButton.setBackground(Color.WHITE);
    whiteButton.addActionListener(this);
    buttonPanel.add(whiteButton);

    JButton blueButton = new JButton("Blue");
    blueButton.setBackground(Color.BLUE);
    blueButton.addActionListener(this);
    buttonPanel.add(blueButton);

    JButton resetButton = new JButton("Reset");
    resetButton.setBackground(Color.LIGHT_GRAY);
    resetButton.addActionListener(this);
    buttonPanel.add(resetButton);

    add(buttonPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e)
{
    String buttonString = e.getActionCommand();

    if (buttonString.equals("Red"))
        redPanel.setBackground(Color.RED);
    else if (buttonString.equals("White"))
        whitePanel.setBackground(Color.WHITE);
    else if (buttonString.equals("Blue"))
        bluePanel.setBackground(Color.BLUE);
    else if (buttonString.equals("Reset"))
        redPanel.setBackground(Color.LIGHT_GRAY);
    else if (buttonString.equals("Reset"))
        bluePanel.setBackground(Color.LIGHT_GRAY);
    else if (buttonString.equals("Reset"))
        whitePanel.setBackground(Color.LIGHT_GRAY);
    else
        System.out.println("Unexpected error.");


}
}

Here was your problem. You had if else's on each panel for the reset. Compare the code below to what you have. It was just a simple logic issue.


    public void actionPerformed(ActionEvent e) {
        String buttonString = e.getActionCommand();

        if (buttonString.equals("Red"))
            redPanel.setBackground(Color.RED);
        else if (buttonString.equals("White"))
            whitePanel.setBackground(Color.WHITE);
        else if (buttonString.equals("Blue"))
            bluePanel.setBackground(Color.BLUE);
        else if (buttonString.equals("Reset")) {
            redPanel.setBackground(Color.LIGHT_GRAY);
            bluePanel.setBackground(Color.LIGHT_GRAY);
            whitePanel.setBackground(Color.LIGHT_GRAY);
        }
        else
            System.out.println("Unexpected error.");

And a couple of suggestions.

  • Don't extend JFrame. Just use an instance of it. It's better technique.
  • Put the following as the last statement in your constructor. It will center the panel on your screen.
setLocationRelativeTo(null);
// or when using a frame instance.
frame.setLocationRelativeTo(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM