英文:
why does my Action event is not being triggered?
问题
我正在尝试构建一个简单的记事本。但由于某种原因,单击“New”菜单项后未触发我的操作事件。下面给出的代码应在用户单击“New”菜单项时调用“newFile()”方法。
public class MainUi implements ActionListener{
//所有变量
public JFrame window;
public JTextArea textArea;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu menuFile, menuEdit, menuFormat;
public JMenuItem fNew, fOpen, fSave, fSaveAs, fExit;
//创建所有功能类的实例
File_functions file = new File_functions(this);
//构造函数以构建界面
public MainUi() {
createWindow();
createMenuBar();
addFileMenu();
createTextArea();
window.setVisible(true);
}
public static void main(String[] args) {
new MainUi();
}
//菜单栏
public void createMenuBar() {
menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
//将文件添加到菜单
menuFile = new JMenu("File");
menuBar.add(menuFile);
}
//所有文件菜单
public void addFileMenu() {
fNew = new JMenuItem("New");
menuFile.add(fNew);
menuFile.addActionListener(this);
menuFile.setActionCommand("New");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command = e.getActionCommand();
System.out.print(command);
switch (command) {
case "New": file.newFile(); break;
}
}
}
在控制台中也没有显示错误,也没有在另一个类(File_function)内部显示错误。
英文:
I am trying to build a simple notepad. But for some reason my action event is not being triggered after clicking on the New menu item. The code given below should call a newFile() method, when user click on New menu item.
public class MainUi implements ActionListener{
//all the variables
public JFrame window;
public JTextArea textArea;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu menuFile, menuEdit, menuFormat;
public JMenuItem fNew, fOpen, fSave, fSaveAs, fExit;
//creating instance of all the functional class
File_functions file = new File_functions(this);
//Constructor to build the ui
public MainUi() {
createWindow();
createMenuBar();
addFileMenu();
createTextArea();
window.setVisible(true);
}
public static void main(String[] args) {
new MainUi();
}
//menu bar
public void createMenuBar() {
menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
//add file to the menu
menuFile = new JMenu("File");
menuBar.add(menuFile);
}
//all the file menu
public void addFileMenu() {
fNew = new JMenuItem("New");
menuFile.add(fNew);
menuFile.addActionListener(this);
menuFile.setActionCommand("New");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command = e.getActionCommand();
System.out.print(command);
switch (command) {
case "New": file.newFile(); break;
}
}
}
There is no error showing in console neither inside another class (File_function).
答案1
得分: 3
fNew = new JMenuItem("新建");
menuFile.add(fNew);
menuFile.addActionListener(this);
// 需要将ActionListener添加到JMenuItem中:
fNew = new JMenuItem("新建");
menuFile.add(fNew);
//menuFile.addActionListener(this);
fNew.addActionListener(this);
注意,无需设置动作命令。它将默认为菜单项的文本。
英文:
fNew = new JMenuItem("New");
menuFile.add(fNew);
menuFile.addActionListener(this);
The ActionListener needs to be added to the JMenuItem:
fNew = new JMenuItem("New");
menuFile.add(fNew);
//menuFile.addActionListener(this);
fNew.addActionListener(this);
Note, there is no need to set the action command. It will default to the text of the menu item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论