简体   繁体   中英

How to change action after the 1st click

I'm stacked!! I am relatively new to java and I have been days searching the web to solve my problem without success, its now time to ask for your help.

I cannot...(actually do not know) how to change the actionlistener (actionPerformed) after the second click in an array of JButtons. Please find the code below so that you can guide me.

This is how the program performs right now.

On the 1st click on the grid, the border of the button clicked will change to 'blue' and another button will change its boarder to 'red'. on the next click the same action is performed.

What I need is to change action when the 'red' bordered button is clicked, lets say change the color of the buttons from the 'red' to the 'blue' buttons.(x 3 buttons).

The logic about how to perform the final result I think I can do on my own but my problem is how to change the action when the red bordered button is clicked.

Your help is much appreciated!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class ChangeGrid
{

    private JButton[][] bu1 = new JButton[10][10];
    public ChangeGrid(JFrame frame)
    {
        super();

        JPanel pCenter = new JPanel();
        JPanel pTop = new JPanel();

        frame.add(pCenter, BorderLayout.CENTER);
        frame.add(pTop, BorderLayout.NORTH);

        JPanel Grid10x10 = new JPanel(new GridLayout(10,10));

        pCenter.add(Grid10x10);

        for(int c = 0; c< 10; c++)
        {
            for (int r = 0; r< 10; r++)
            {
                bu1[c][r] = new JButton("X");
                Grid10x10.add(bu1[c][r]);

                final int i = c;
                final int j = r;

                bu1[i][j].addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int f = (3-1);

                        bu1[i][j].setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));

                        if ((j+f)<=9)
                        {
                            bu1[i][j+f].setBorder(BorderFactory.createLineBorder(Color.RED, 2));
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null,"move your 1st click more to the center");
            }
                    }
                });
            }
        }
    }
}

import javax.swing.JFrame;
public class GUIFrame extends JFrame
{
    public GUIFrame()
    {
        super("Grid 10 x 10");

        this.setSize(1350, 700);
        this.setVisible(true);
        this.setResizable(true);
        this.setDefaultCloseOperation(GUIFrame.EXIT_ON_CLOSE);
    }
}

import javax.swing.JFrame;
public class Main
{
    public static void main(String args[])
        {

            JFrame frame = new GUIFrame();
            ChangeGrid pC = new ChangeGrid(frame);
            frame.pack();
        }

}

You can change your ActionListener like next:

bu1[i][j].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int f = (3 - 1);

                    Border border = bu1[i][j].getBorder();
                    if(border instanceof LineBorder){
                        LineBorder lBorder = (LineBorder) border;
                        if(lBorder.getLineColor().equals(Color.RED)){
                            //red border clicked
                            redBorderAction(bu1[i][j]);
                            return;
                        }
                    }
                    defaultAction(i,j,f);       
                }
            });

And other methods:

private void defaultAction(int i, int j, int f) {
    bu1[i][j].setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));

    if ((j + f) <= 9) {
        bu1[i][j + f].setBorder(BorderFactory.createLineBorder(Color.RED, 2));
    } else {
        JOptionPane.showMessageDialog(null,"move your 1st click more to the center");
    }
}

private void redBorderAction(JButton btn) {
    btn.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
}

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