我正在制作一个棋盘游戏,8X8 矩阵,框架中有 64 个 JButton。 到目前为止我的代码是这样的:
public class Main {
static JFrame f = new JFrame();;
static JButton btn;
static JButton btnTemp;
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout((new GridLayout(8,8)));//size of the board
f.setTitle("ex");
f.setSize(800,800);
for (int i=0;i<=7;i++)
{
for (int j = 0; j<=7;j++)
{
btn=new JButton();
btn = new JButton(new SoliderW());
btn.setName("btn"+i+""+j);
btn.setBackground(Color.BLACK);
btn.addActionListener(actionListener); // make a listener to the button
f.add(btn);
}
}
f.setVisible(true);
我试图使用以下代码判断哪个 JButoon 被单击:
Component[] components = f.getContentPane().getComponents();
ActionListener actionListener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello");
}
};
for (Component component : components)
{
if (component instanceof JButton)
{
((JButton) component).addActionListener(actionListener);
}
}
但是,我不明白如何判断单击了哪个 Jbutton。
最佳答案
让我们从 static
开始吧,它不是你的 friend ,你应该避免使用它,特别是当你尝试跨对象边界引用实例时。
你可以...
使用Anonymous Classes ,例如...
btn = new JButton();
btn = new JButton(new SoliderW());
btn.setName("btn" + i + "" + j);
btn.setBackground(Color.BLACK);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// Do some work
}
}); // make a listener to the button
但是,说实话,由于 btn
是静态
,这对您没有帮助
你可以...
利用actionCommand
属性
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
// Do more work
}
};
//...
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
btn = new JButton();
btn = new JButton(new SoliderW());
btn.setName("btn" + i + "" + j);
btn.setBackground(Color.BLACK);
// Replace the text with something that will
// uniquely identify this button
btn.setActionCommand("some cell identifier");
btn.addActionListener(actionListener); // make a listener to the button
f.add(btn);
}
}
你可以...
创建一个自定义的ActionListener
,它获取所需的信息,以便它可以更好地决定要做什么(并将其与按钮本身解耦)
public class CardActionListener implements ActionListener {
private int row, col;
public CardActionListener(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// Do some work...
}
}
//...
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
btn = new JButton();
btn = new JButton(new SoliderW());
btn.setName("btn" + i + "" + j);
btn.setBackground(Color.BLACK);
btn.addActionListener(new CardActionListener(i, j)); // make a listener to the button
f.add(btn);
}
}
你可以...
我个人偏好使用Action
API .
这与上一个建议类似,但创建了一个更加独立的工作单元,它与调用者分离。
public class CardAction extends AbstractAction {
private int row, col;
public CardAction(int row, int col) {
this.row = row;
this.col = col;
putValue(Action.LARGE_ICON_KEY, new SoliderW());
}
@Override
public void actionPerformed(ActionEvent evt) {
// Do some work...
}
}
//...
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
btn = new JButton(new CardAction(i, j));
f.add(btn);
}
}
什么是重要的...
我正在尝试的事情之一是将操作功能与按钮本身分离,因此操作不依赖于按钮,而是依赖于提供执行其操作所需的信息。
这是“模型- View - Controller ”的核心概念,将使您的代码更易于维护
关于java - 检查(多个 Jbutton 中的)哪个 Jbutton 被单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59415875/