java - 设置 Action 监听器并更改背景

标签 java swing

我创建了 3 个按钮。它们各自在 JFrame 中显示两次。我在更改框架背景时遇到问题。我已经设置了 ActionListeners 但单击后没有任何变化。我可以寻求帮助吗?

public class MyButtons extends JPanel {
    public static JFrame frame;
    private JButton Red = new JButton("Red");
    private JButton Green = new JButton("Green");
    private JButton Blue = new JButton("Blue");

    public void InitializeButton()
    {       
        Blue.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            frame.setBackground(Color.BLUE);
          }
        });

        Green.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            frame.setBackground(Color.GREEN);
          }
        });

        Red.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            frame.setBackground(Color.RED);
          }
        });
    }

    public MyButtons()  {       
        InitializeButton();
        add(Red);
        add(Green);
        add(Blue);      
    }   

    public static void main(String[] args) {
        frame = new JFrame();


        JPanel row1 = new MyButtons();
        JPanel row2 = new MyButtons();

        row1.setPreferredSize(new Dimension(250, 100));
        row2.setPreferredSize(new Dimension(250, 100));

        frame.setLayout(new GridLayout(3,2));
        frame.add(row1);
        frame.add(row2);
        frame.pack();        
        frame.setVisible(true);
    }

   }

最佳答案

此代码可以工作,但可能不符合您的预期:

enter image description here

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

public class MyButtons extends JPanel {

    //public static JFrame frame;
    // static is rarely a solution of problems in a GUI. ToDo! Change!
    static JPanel ui = new JPanel(new GridLayout(2, 0, 20, 20));

    private JButton Red = new JButton("Red");
    private JButton Green = new JButton("Green");
    private JButton Blue = new JButton("Blue");

    public void InitializeButton() {
        Blue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.BLUE);
            }
        });

        Green.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.GREEN);
            }
        });

        Red.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.RED);
            }
        });
    }

    public MyButtons() {
        InitializeButton();
        add(Red);
        add(Green);
        add(Blue);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(250, 100);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        JPanel row1 = new MyButtons();
        JPanel row2 = new MyButtons();

        //row1.setPreferredSize(new Dimension(250, 100));
        //row2.setPreferredSize(new Dimension(250, 100));

        //frame.setLayout(new GridLayout(3, 2,10,10));
        ui.add(row1);
        ui.add(row2);
        frame.add(ui);
        frame.pack();
        frame.setVisible(true);
    }
}

注意请学习常见的 Java 命名法(命名约定 - 例如 EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT )并始终如一地使用它。

关于java - 设置 Action 监听器并更改背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442878/

相关文章:

java - 使用递归查找堆栈中的最小值

java - JGit/EGit 加载翻译包失败 zh_CN

Java Server Faces - 导入图像

java - Epson TM-T70 Java打印

用于大写的 Java Swing DocumentFilter

java - 如何知道构建是否已完成且没有失败

java - repaint() 绘制速度比paintComponent() 慢?

java - 在java swing中从一帧移动到另一帧

java - JComboBox 每个项目的颜色不同失败

java - Play Framework 2.2.1 Ebean 3个表之间的多对多关系