简体   繁体   English

第一次点击后如何更改动作

[英]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. 我对java相对较新,并且我已经搜索Web几天以解决我的问题而没有成功,现在该寻求您的帮助了。

I cannot...(actually do not know) how to change the actionlistener (actionPerformed) after the second click in an array of JButtons. 我无法...(实际上不知道)在JButtons数组中的第二次单击后如何更改actionlistener(actionPerformed)。 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). 我需要的是单击“红色”边框按钮时更改操作,可以说将按钮的颜色从“红色”按钮更改为“蓝色”按钮(x 3个按钮)。

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: 您可以像下面那样更改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);       
                }
            });

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

暂无
暂无

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

相关问题 如何仅在第一个片段上隐藏浮动操作按钮? - how to hide the float action button only on the 1st fragment? 单击第一个项目时,Android CarouselPicker - Android CarouselPicker when 1st item is click Java awt计算器+/-,如何用-替换+:第一次单击=“-”,第二次单击=“ +”,依此类推 - Java awt calculator +/- , how to replace + with - : 1st click = “-”, 2nd click = “+” and so on 如何通过单击第一个Java Windows应用程序(Jframe)中的菜单项启动第二个Jframe - How to start 2nd Jframe by click menu item in 1st java windows application (Jframe) 如何获得有关按钮单击第一活动的信息并在第三活动中使用它的信息? -安卓 - How to get information about a button click on 1st activity and use it in the 3rd activity? - android 如何获得1983年1月1日以后加入的部门明智员工的人数 - how to get no of employees dept wise who joined after 1st jan 1983 如何在逗号后反转字符串,然后打印字符串Java的第一部分 - How to reverse a String after a comma and then print the 1st half of the String Java 在Runtime.exec()延迟一段时间后,如何将第二个命令附加到第一个命令? - How to append a 2nd command to the 1st one after some delay in Runtime.exec()? 如何使用BufferedReader读取文件的第一行? - How to Read 1st line of a file with BufferedReader? 如何过滤哈希图并查找第一次出现 - how to filter hashmap and find 1st occurance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM