簡體   English   中英

一鍵更改多個 JPanel 的顏色

[英]Using one button to change the color of multiple JPanels

所以我有三個面板,我有三個不同的按鈕,用於將它們分別更改為各自的 colors。 我需要添加第四個按鈕,它將所有三個面板恢復為原始默認淺灰色。 我添加了這個“重置”按鈕,它只改變了第一個面板。 我究竟做錯了什么?

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


}
}

這是你的問題。 您在每個面板上都有 if else's 用於重置。 將下面的代碼與您擁有的代碼進行比較。 這只是一個簡單的邏輯問題。


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM