简体   繁体   English

如何正确地将ActionListener实现为Composite按钮类?

[英]How to correctly implement a ActionListener to a Composite button class?

I have the button turn black and I print Hello to make sure when it works. 我把按钮变黑了,我打印Hello以确保它何时工作。 Nothing is being printed and even the color id not changing, yet there is no error. 什么都没有被打印,甚至颜色不会改变,但没有错误。

The 3 classes I need are below any help is appreciated. 我需要的3个课程以下任何帮助表示赞赏。 I am new to Java swing and any tips are welcome. 我是Java swing的新手,欢迎任何提示。

Button Class 按钮类

 import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class RedSpace implements Space, ActionListener {
    private int x;
    private boolean isPressed;
    private int y;
    private JButton button;

    public RedSpace(int x, int y){
        button = new JButton();

        button.setBackground(Color.RED);
        this.x = x;
        this.y = y;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    @Override
    public JButton getButton() {
        return button;
    }

    @Override
    public boolean getIsPressed() {
        return isPressed;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(button)) {
        System.out.println("Hello");
            isPressed = true;
            button.setBackground(Color.BLACK);
                }

    }

Space Interface 空间接口

 import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JToggleButton;


public interface Space extends ActionListener {
    int x = 0;
    int y = 0;
    JButton button = new JButton();
    boolean isPressed = false;

    int getX();

    int getY();

    JButton getButton();

    boolean getIsPressed();

}

Board Class 董事会类

 public class Board {

    private static int x;
    private static int y;
    private static JFrame frame;
    private static JPanel pane;
    static ArrayList<ArrayList<Space>> buttons = new ArrayList<ArrayList<Space>>();
    static ArrayList<Space> buttonsRow;

    public Board(int x, int y){
        this.x = x;
        this.y = y;
    }

    public static void printBoard(){
        frame = new JFrame();
        frame.setTitle("GameBoard");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new CardLayout());
        pane = new JPanel();
        pane.setLayout(new GridLayout(8,8));
        frame.add(pane);
        for(int i = 0; i < x;i++){
            buttonsRow = new ArrayList<Space>();
            for(int ii = 0; ii<y;ii++){

                if(i == 0 && ((ii-1)%2 != 0))
                    buttonsRow.add(new BlueSpace(i,ii));
                else if(i == 1 && ((ii-1)%2 == 0))
                    buttonsRow.add(new BlueSpace(i,ii));                
                else if(i == 6 && ((ii-1)%2 != 0))
                    buttonsRow.add(new RedSpace(i,ii));
                else if(i == 7 && ((ii-1)%2 == 0))
                    buttonsRow.add(new RedSpace(i,ii));
                else buttonsRow.add(new WhiteSpace(i,ii));

                pane.add(buttonsRow.get(ii).getButton());

            }
            buttons.add(buttonsRow);
        }
        frame.setSize(new Dimension(900,900));
        pane.setSize(new Dimension(900,900));

        frame.setVisible(true);


    }


}

Nothing is being printed or changing color bu there is no error. 没有任何东西被打印或改变颜色没有错误。

You need to add the ActionListener to the button. 您需要将ActionListener添加到按钮。

button = new JButton();
button.addActionListener( this );

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

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