[英]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.