简体   繁体   中英

Can I use actionListeners on JPanels?

I am trying to add and actionListener to a JPanel it's self but keep getting the error"cannot find symbol". I was just wondering if it is possible to do this as I want to be able to click on the panel and make the colour change. Any help would be appreciated.

Here is what i have so far.

import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import java.awt.Color.*;
import java.awt.event.*;
/**
 * Write a description of class SimpleFrame here.
 *
 * @author OFJ2
 * @version 
 */
public class Game extends JFrame 
            implements ActionListener
{
private final int ROWS = 5;
private final int COLUMS = 2;
private final int GAP = 2;
private final int SIZE = ROWS * COLUMS;
private int x;
private JPanel leftPanel = new JPanel(new GridLayout(ROWS,COLUMS, GAP,GAP));
private JPanel [] gridPanel = new JPanel[SIZE];
private JPanel middlePanel = new JPanel();    
private JPanel rightPanel = new JPanel();
private Color col1 = Color.GREEN;
private Color col2 = Color.YELLOW;
private Color tempColor;


public Game()
{
    super("Chasing Bombs OFJ2");
    setSize(200,200);
    setVisible(true);
    makeFrame();
}


public void makeFrame()
{
    Container contentPane = getContentPane();
    contentPane.setLayout(new GridLayout());
    leftPanel.setLayout(new GridLayout(ROWS, COLUMS));

    //JLabel label2 = new JLabel("Pocahontas");

    JButton playButton = new JButton("Play Game");
    JButton exitButton = new JButton("Exit");


    JButton easyButton = new JButton("Easy");
    JButton mediumButton = new JButton("Medium");
    JButton hardButton = new JButton("Hard");


    add(leftPanel);

    add(middlePanel, new FlowLayout());

    add(rightPanel);

    setGrid();


    middlePanel.add(playButton);
    middlePanel.add(exitButton);
    rightPanel.add(easyButton);
    rightPanel.add(mediumButton);
    rightPanel.add(hardButton);
    leftPanel.setBackground(Color.PINK);
    middlePanel.setBackground(Color.RED);

    easyButton.addActionListener(this);
    mediumButton.addActionListener(this);
    hardButton.addActionListener(this);
    playButton.addActionListener(this);
    exitButton.addActionListener(this);



}

public void setGrid()
{
    for(int x = 0; x < SIZE; x++) {
           gridPanel[x] = new JPanel();
           gridPanel[x].setBorder(BorderFactory.createLineBorder(Color.BLACK));
           leftPanel.add(gridPanel[x]);
           gridPanel[x].setBackground(Color.GREEN);
           gridPanel[x].addActionListener(this);

        }
}

@Override
public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if (source == gridPanel[0]){
        gridPanel[x].setBackground(Color.BLACK);
    }
}
}

I have tried to find if there is any other method that is needed to do this but cant find anything. Is it possible that I will have to add a button to fill each of the panels to make this work?

Thanks!

It defaults to no addActionListener, and the code below is a reference suggestion.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

class GPanel extends JPanel {
    private List<ActionListener> listenerList = new ArrayList<>();

    public GPanel() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                var event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "GPanel");
                for (var i : listenerList) {
                    i.actionPerformed(event);
                }
            }
        });
    }

    public void addActionListener(ActionListener listener) {
        listenerList.add(listener);
    }
}

public class ActionListenerTest extends JFrame {
    public static void main(String[] args) {
        new ActionListenerTest();
    }

    public ActionListenerTest() {
        GPanel test = new GPanel();
        test.setBackground(Color.BLUE);
        add(test);
        test.addActionListener(e-> {
            System.out.println("Click: " + e.getActionCommand());
        });
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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