所以,我有一个 JTextArea。我在它的输入/动作映射中添加了键盘动作。
在输入按下时,应该创建 JDialog 及其内容。而且我需要将 keyListener 添加到它将包含的按钮,我不能,因为该按钮没有最终修饰符。如果我将其设置为 final,我将无法编辑它的属性。
这是代码片段:
class blabla extends JTextArea
{
getInputMap.put(KeyStroke.getKeyStroke("ENTER"), "pressedEnter");
getActionMap.put("pressedEnter", new AbstractAction()
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e)
{
JDialog dialog;
JButton confirm;;
//JDialog
dialog = new JDialog(Main.masterWindow, "newTitle", true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
dialog.addWindowListener(new WindowAdapter()
{
public void windowActivated(WindowEvent e)
{
//this doen't work, it asks me to declare confirm as final
//and I have to request focuse here due to Java bug
confirm.requestFocus();
}
});
//JButton for confirming
confirm = new JButton(lang.getString("ok"));
confirm.setAlignmentX(Component.CENTER_ALIGNMENT);
confirm.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
//this doen't work, it asks me to declare confirm as final
confirm.doClick();
}
}
});
dialog.add(confirm);
dialog.pack();
dialog.setLocationRelativeTo(Main.masterWindow);
dialog.setVisible(true);
}
我怎样才能使这项工作?