使用一个按钮更改多个JPanel的颜色

问题描述 投票:2回答:1

所以我有三个面板,我有三个不同的按钮,用于将它们分别更改为各自的颜色。我需要添加第四个按钮,这会将所有三个面板恢复为其原始的默认浅灰色。我添加了这个“重置”按钮,它只将第一个面板改回来。我究竟做错了什么?

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.");


}
}
java user-interface jpanel
1个回答
1
投票

这是您的问题。您是否在每个面板上都设置了其他选项。将下面的代码与您拥有的代码进行比较。这只是一个简单的逻辑问题。


    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.");

和一些建议。

  • 不扩展JFrame。只需使用它的一个实例。这是更好的技术。
  • 将以下内容作为构造函数中的最后一条语句。它将面板居中显示在屏幕上。
setLocationRelativeTo(null);
// or when using a frame instance.
frame.setLocationRelativeTo(null);
© www.soinside.com 2019 - 2024. All rights reserved.