如何在第一次点击后更改操作

时间:2014-08-03 08:53:07

标签: java swing actionlistener actionevent

我堆叠了!!我是一个相对较新的java,我一直在网上搜索解决我的问题没有成功,现在是时候请求你的帮助了。

我不能......(实际上不知道)如何在JButton数组中第二次单击后更改actionlistener(actionPerformed)。请找到下面的代码,以便您可以指导我。

这就是程序现在的表现。

在第一次点击网格时,点击按钮的边框将变为“蓝色”'另一个按钮会将其边界改为红色'。在下一次单击时执行相同的操作。

我需要的是在红色'单击有边框的按钮,让我们说从红色'红色'更改按钮的颜色。到了蓝色'按钮。(x 3个按钮)。

关于如何执行最终结果的逻辑我认为我可以自己做,但我的问题是如何在点击红色边框按钮时更改动作。

非常感谢您的帮助!

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

}

1 个答案:

答案 0 :(得分:0)

您可以像下一个更改ActionListener

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

和其他方法:

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