简体   繁体   English

设置动作监听器并更改背景

[英]Setting Action Listener and changing background

I've created 3 buttons. 我创建了3个按钮。 They are each displayed twice in a JFrame. 它们在JFrame中分别显示两次。 I'm having trouble changing the background of the frame. 我在更改框架背景时遇到问题。 I've set ActionListeners but upon clicking, nothing is changing. 我已经设置了ActionListeners,但是单击时没有任何变化。 May I ask for some help. 我可以寻求帮助吗?

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

   }

This code works, but probably not as you expected: 该代码有效,但可能与您预期的不一样:

在此处输入图片说明

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

NB Please learn common Java nomenclature (naming conventions - eg EachWordUpperCaseClass , firstWordLowerCaseMethod() , firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT ) and use it consistently. 注意:请学习通用的Java命名法(命名约定,例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute除非它是UPPER_CASE_CONSTANT ),并始终使用它。

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

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