简体   繁体   English

在 JButton 不更新的情况下更改 JFrame 背景颜色

[英]Change JFrame background color with JButton not updating

I'm trying to make a very simple GUI that updates the background color upon clicking a button.我正在尝试制作一个非常简单的 GUI,在单击按钮时更新背景颜色。 I can't for the life of me figure out what is wrong with my code.我一生都无法弄清楚我的代码有什么问题。 When it is run nothing updates upon clicking the buttons, any help would be appreciated!当它运行时,单击按钮没有更新,任何帮助将不胜感激!

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class ChallengeGUI extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JButton blue;
    private JButton red;
    private JButton green;


    public ChallengeGUI() {

        JButton blue = new JButton("BLUE");
        blue.addActionListener(this);
        add(blue);

        JButton red = new JButton("RED");
        red.addActionListener(this);
        add(red);

        JButton green = new JButton("GREEN");
        green.addActionListener(this);
        add(green);

        setLayout(new FlowLayout());
        setSize(600,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == blue) {
            getContentPane().setBackground(Color.BLUE);
        } else if (e.getSource() == red) {
            getContentPane().setBackground(Color.RED);
        } else if (e.getSource() == green) {
            getContentPane().setBackground(Color.GREEN);
        }

    }

    public static void main(String[] args) {
        new ChallengeGUI();

    }

}

You have one mistake.你有一个错误。

  • You've shadowed your buttons so e.getSource()==red is always false.你已经隐藏了你的按钮,所以 e.getSource()==red 总是错误的。

Instead of代替

JButton red = new JButton("RED");

just write写吧

red = new JButton("RED");

When you wrote: JButton red =... it created a local variable named red separate from the field declared in your class, also named red .当您写道: JButton red =...它创建了一个名为red的局部变量,与您在 class 中声明的字段(也称为red )分开。 That means the field did not get initialized.这意味着该字段没有被初始化。 In your listener, none of the cases were true because red , blue , and green were null and e.getSource() was returning the local JButton you created.在您的听众中,没有一种情况是真实的,因为redbluegreen是 null 并且e.getSource()正在返回您创建的本地 JButton。

You can write your actionPerformed() method like this:您可以像这样编写您的actionPerformed()方法:

public void actionPerformed(ActionEvent e) {
    String actionCommand = e.getActionCommand();
    Color color;
    switch (actionCommand) {
        case "BLUE":
            color = Color.BLUE;
            break;
        case "RED":
            color = Color.RED;
            break;
        case "GREEN":
            color = Color.GREEN;
            break;
        default:
            color = null;
            JOptionPane.showMessageDialog(null, "Unhandled: " + actionCommand);
    }
    if (color != null) {
        getContentPane().setBackground(color);
    }
}

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

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