这个小程序的功能是: 单击一个按钮,单击红色边框按钮并更改操作(更改按钮的颜色 - 从红色边框按钮到蓝色边框按钮)...但即使JOptionPane也可以,只要我设法更改行动。
请查看Panel Class的已审核代码:....它仍无效。
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 Panel
{
private JButton[][] bu1 = new JButton[10][10];
public Panel(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;
ActionListener[] listeners = bu1[i][j].getActionListeners();
for (ActionListener l : listeners)
{
bu1[i][j].removeActionListener(l);
}
bu1[i][j].addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
int f = (3-1);
Object redButton = new Object();
if(e.getSource() == redButton)
{
JOptionPane.showMessageDialog(null,"row ");
}
bu1[i][j].setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
if ((j+f)<=9)
{
bu1[i][j+f].setBorder(BorderFactory.createLineBorder(Color.RED, 2));
redButton = bu1[i][j+f];
}
else
{
JOptionPane.showMessageDialog(null,"move your 1st click more to the center");
}
}
});
}
}
}
}
我堆积!!我是一个相对较新的java,我一直在网上搜索解决我的问题没有成功,现在是时候请求你的帮助了。
我不能......(实际上不知道)如何在JButton数组中第二次单击后更改actionlistener(actionPerformed)。请找到下面的代码,以便您可以指导我。
在第一次单击网格时,单击按钮的边框将变为“蓝色”,另一个按钮将其边框更改为“红色”。当点击“红色”边框按钮时,我需要更改操作,让我们更改按钮的颜色,从“红色”按钮更改为“蓝色”按钮。
非常感谢您的帮助!
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();
}
}
答案 0 :(得分:1)
在最终目标上并不完全清楚,但要更改动作侦听器,首先需要删除当前侦听器并添加新侦听器。
ActionListener[] listeners = bu1[i][j].getActionListeners();
for (ActionListener l : listeners) {
bu1[i][j].removeActionListener(l);
}
bu1[i][j].addActionListener(new ActionListener() {
...
});
但是,如果您将对红色的按钮的引用保持为单独的变量,那可能会更好。而不是更改动作侦听器只是检查当前按钮是否是当前红色的按钮
public void actionPerformed(ActionEvent e)
{
...
if(e.getSource() == redButton){
//do something with red button
}
if ((j+f)<=9)
{
bu1[i][j+f].setBorder(BorderFactory.createLineBorder(Color.RED, 2));
redButton = bu1[i][j+f];
}
...
}
答案 1 :(得分:0)
if(e.getClickCount()==1)
{
}
if(e.getClickCount()==2)
{
}
答案 2 :(得分:0)
另一个解决方案是使用propertyChangeListener
制作自己的属性,示例代码
button.addPropertyChangeListener("grid",new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getNewValue());
}
});