简体   繁体   English

为什么我会收到此错误?

[英]Why do i get this error?

this is the snippet that produces an error saying : 这是产生错误的片段:

')' expected
';' expected
not a statement
cannot find symbol
symbol : variable ActionEvent

The snippet : 片段:

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser chooseToAdd = new JFileChooser();
    int option = chooseToAdd.showOpenDialog(this);
    if( option == JFileChooser.APPROVE_OPTION ) {
        nameOfAudioFile = chooseToAdd.getSelectedFile().getAbsolutePath();
        //clonejTree1ValueChanged( TreeSelectionEvent evt );
        tester(java.awt.event.ActionEvent evt);
    }
}

private void tester(java.awt.event.ActionEvent evt) {
    System.out.println("tester");
}

Is there any syntax error ? 有语法错误吗?

This method invocation is wrong: 这个方法调用是错误的:

tester(java.awt.event.ActionEvent evt);

That's trying to declare a parameter, but you need to be passing an argument. 那是试图声明一个参数,但你需要传递一个参数。 I suspect you want: 我怀疑你想要:

tester(evt);

It's important to understand the difference between a method declaration (which declares parameters with names and types) and a method invocation (which supplies values for those parameters). 理解方法声明 (使用名称和类型声明参数)和方法调用 (为这些参数提供值)之间的区别非常重要。

As an aside, assuming you're quite new to Java I would recommend not starting off with Swing or any other kind of UI. 顺便说一下,假设你对Java很陌生,我建议不要从Swing或任何其他类型的UI开始。 I would suggest you start off writing some simple console apps that let you get to grips with the basic syntax of Java without having to worry about all the complexities introduced by user interfaces. 我建议你开始编写一些简单的控制台应用程序,让你掌握Java的基本语法,而不必担心用户界面引入的所有复杂性。

This line is wrong: 这条线错了:

tester(java.awt.event.ActionEvent evt);

Here you should pass an object of the type ActionEvent. 在这里,您应该传递ActionEvent类型的对象。

So it should be something like this: 所以它应该是这样的:

tester(evt);

Call 呼叫

tester(evt); 

instead of 代替

tester(java.awt.event.ActionEvent evt);

You shouldn't define the type there, it is already known. 你不应该在那里定义类型,它已经知道了。 Method invocation syntax is not the same as method definition. 方法调用语法与方法定义不同。

If you call a method, you don't have to provide the type of the parameter. 如果调用方法,则不必提供参数类型。 So in line 6 of the snippet it only should be 所以在代码片段的第6行中它应该是唯一的

tester(evt);

从中删除类型定义

tester(java.awt.event.ActionEvent evt);    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM