簡體   English   中英

如何正確地將ActionListener實現為Composite按鈕類?

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

我把按鈕變黑了,我打印Hello以確保它何時工作。 什么都沒有被打印,甚至顏色不會改變,但沒有錯誤。

我需要的3個課程以下任何幫助表示贊賞。 我是Java swing的新手,歡迎任何提示。

按鈕類

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

    }

空間接口

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

}

董事會類

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


    }


}

沒有任何東西被打印或改變顏色沒有錯誤。

您需要將ActionListener添加到按鈕。

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

暫無
暫無

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

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