为什么我的动作事件没有被触发?

huangapple go评论58阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月3日 23:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定