简体   繁体   English

一键更改多个 JPanel 的颜色

[英]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.所以我有三个面板,我有三个不同的按钮,用于将它们分别更改为各自的 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.您在每个面板上都有 if else's 用于重置。 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.不要扩展 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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