我正在使用 AWT 在 Java 中设计一个记事本应用程序。现在,我已经创建了 MenuBar 和 MenuItems,但我无法得到的是如何停用特定的 MenuItem,就像我们没有在 TextArea 中写任何东西一样,在编辑部分中的剪切和复制选项,以及撤消选项保持停用状态。 java中有什么方法可以做这些事情吗?我正在使用 AWT 而不是 Swing 这样做。
最佳答案
在反复试验和网上冲浪之后得到了答案。在构造函数中初始化它们时,我们会将它们的初始值保持为“false”。因此,当文本编辑器打开时,屏幕上没有任何内容。因此它们将显示为禁用,然后使用 KeyListener 注册 TextArea 对象将完成启用该菜单项的工作。 ;)
constructor()
{
undo=new MenuItem("Undo",new MenuShortcut(90));
edit.add(undo);
un.setEnabled(false);
cut=new MenuItem("Cut",new MenuShortcut(88));
edit.add(cut);
cut.setEnabled(false);
copy=new MenuItem("Copy",new MenuShortcut(67));
edit.add(copy);
copy.setEnabled(false);
paste=new MenuItem("Paste",new MenuShortcut(86));
ed.add(paste);
selectAll=new MenuItem("Select All",new MenuShortcut(65));
ed.add(selectAll);
selectAll.setEnabled(false);
textArea.addKeyListener();
}
public void keyTyped(KeyEvent arg0)
{
if(textArea.getText()!=null)
{
cut.setEnabled(true);
copy.setEnabled(true);
undo.setEnabled(true);
selectAll.setEnabled(true);
find.setEnabled(true);
}
}
关于java - 在基于 Java 的记事本中停用记事本中的 "Undo"之类的 MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908441/