上个月,我用我对 Java 所知甚少的知识编写了一个可爱的 Tictactoe 游戏。它的一个组成部分在下面的 3 个代码片段中:
// from the constructor ...
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 3));
guiFrame.add(buttonPanel);//, BorderLayout.CENTER);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
button[i][j] = addButton(buttonPanel, i, j);
// ...
private JButton addButton (Container parent, int row, int col) {
int v = (10 * (row + 1) + col + 1); // 11, 12, 13, 21, 22, 23, ... 33
String s = " " + v;
JButton cell = new JButton();
parent.add(cell);
cell.setActionCommand(s);
cell.addActionListener(this);
return cell;
}
public void actionPerformed(ActionEvent e) {
// ... snip
String r = e.getActionCommand().substring(1, 2); // row 1, 2, or 3
String c = e.getActionCommand().substring(2, 3); // col 1, 2, or 3
try {
row = Integer.parseInt(r); col = Integer.parseInt(c);
}
catch (Exception ex) { }
}
它的要点是将(行,列)信息存储到每个单元格的“操作命令”中,以便我稍后可以使用 getActionCommand
来判断哪个单元格包含 X 或 O (通过未显示的逻辑)。
可能是更好的方法;不管怎样,它有效。
现在我要处理一个 11x11 网格,它或多或少是一个填字游戏网格。我想使用类似的逻辑来确定刚刚按下哪个 JTextField
按键组合,以确定是否将背景设置为黑色。以下是添加单元格的非常相似的逻辑:
private JTextField addCell (Container parent, int row, int col) {
JTextField cell;
cell = new JTextField();
int v = (100 * (row + 1) + col + 1);
// v will go 101,102,...111; 201,202,...,211; ... 1101,1102,...1111
String s = "" + v;
cell.setActionCommand(s);
parent.add(cell);
cell.addKeyListener(this);
cell.addActionListener(this);
return cell;
}
但是我找不到类似于 JTextField
监听器事件的 getActionCommand
的内容。
这是我从 public void keyPressed(KeyEvent evt) 中的
:System.out.println("keyPressed "+ evt.getSource());
语句得到的内容(为便于阅读而进行了很大的编辑) )
keyPressed JTextField
[
, 5, 5, 34x32,
layout = BasicTextUI$UpdateHandler,
alignmentX = 0. 0, alignmentY = 0. 0,
border = BorderUIResource$CompoundBorderUIResource@f864fe,
flags = 296,
maximumSize = , minimumSize = , preferredSize = ,
caretColor = PrintColorUIResource [r = 51, g = 51, b = 51],
disabledTextColor = ColorUIResource [r = 184, g = 207, b = 229],
editable = true,
margin = InsetsUIResource [top = 0, left = 0, bottom = 0, right = 0],
selectedTextColor = PrintColorUIResource [r = 51, g = 51, b = 51],
selectionColor = ColorUIResource [r = 184, g = 207, b = 229],
columns = 0, columnWidth = 0,
command = 101,
horizontalAlignment = LEADING
]
而她吹了,在最后一行旁边,command = 101
,正是我需要得到的,但“命令”无论如何都不可用试过了。
你的想法/建议/hellllllp?
最佳答案
如果您想知道哪个组件生成了该事件,您应该使用该事件来获取源:
e.getSource();
操作命令仅适用于 ActionListener(在这种情况下,您将使用 ActionEvent 的 getActionCommand() 方法)而不是 KeyListener。
编辑:
创建一个 map 来保存您需要的信息:
Map<Component, Point> textFields = new Hashmap<Component, Point>();
现在在 addCell 方法中将数据添加到 map :
textFields.put(cell, new Point(row, col));
然后在 KeyListener 中访问该点:
Point whatever = textFields.get(event.getComponent());
现在您知道输入文本的文本字段的行/列。
或者,正如 MadProgrammer 已经建议的那样,您可以将 Point 添加为每个 JTextfield 的客户端属性。这种方法可能比这个建议更容易一些。但是,该方法将为每个 JTextField 创建一个 Map,并且该方法仅为所有文本字段创建一个 Map,因此效率更高。
关于java - 如何从 JTextField 获取 "getActionCommand"?我在调试时看到它但是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19305253/