I am unable to find the mistake. Eclipse is giving me this error. Every {
}
is matching. Please help.
我找不到这个错误。Eclipse给了我这个错误。每一个{}都是匹配的。请帮助。
Multiple markers at this line - Syntax error on token “)”, ; expected - Syntax error on token “(”, { expected
这一行的多个标记——标记上的语法错误“)”;期望-标记的语法错误"(",{期望
My code is as follows :
我的代码如下:
import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
class BackgroundImageJFrame1 extends JFrame {
public BackgroundImageJFrame1() {
JButton b1;
JLabel l1;
final JFileChooser fc = null;
final JTextField textField;
final JButton jb1 = null;
setTitle("Parallel Session");
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
getContentPane().setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("giphy.gif")));
getContentPane().setLayout(new FlowLayout());
l1 = new JLabel("Parallel Simulation Model");
l1.setForeground(Color.RED);
l1.setFont(new Font("Palatino Linotype", Font.BOLD, 20));
l1.setToolTipText("Getting Started");
l1.setBounds(86, 54, 219, 45);
b1 = new JButton("\n\n LET'S GO!");
b1.setVerticalAlignment(SwingConstants.BOTTOM);
b1.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 14));
b1.setForeground(Color.BLUE);
b1.setBackground(Color.YELLOW);
b1.setToolTipText("To begin click here!");
b1.setBounds(120, 210, 272, 186);
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setToolTipText("Progress");
progressBar.setBounds(130, 207, 146, 14);
getContentPane().add(l1);
getContentPane().add(b1);
getContentPane().add(progressBar);
// Just for refresh :) Not optional!
setSize(399, 399);
setSize(400, 400);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame Frame1 = new JFrame();
final JPanel panel1 = new JPanel();
JLabel j1 = new JLabel("Select your model");
panel1.add(j1);
JTextField jt1 = new JTextField();
jt1.setPreferredSize(new Dimension(160, 20));
panel1.add(jt1);
new JButton("Browse");
jb1.setPreferredSize(new Dimension(100, 20));
panel1.add(jb1);
JLabel label1 = new JLabel("Enter the number instances");
Frame1.setVisible(true);
Frame1.setSize(300, 300);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(label1);
Frame1.getContentPane().add(panel1);
panel1.setBackground(Color.CYAN);
final JTextArea t1 = new JTextArea("Enter here");
panel1.add(t1);
JButton b11 = new JButton("SET");
panel1.add(b11);
jb1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBrowseActionPerformed(null);
}
});
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
if (fc == null) {
fc = new JFileChooser(".");
}
// Show it.
int returnVal = fc.showOpenDialog(null);
// Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
textField.setText(fc.getSelectedFile().getPath());
} else {
textField.setText("");
}
// Reset the file chooser for the next time it's shown.
fc.setSelectedFile(null);
}
//done
b11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String a = t1.getText();
System.out.println(" " + a);
try {
int itr = Integer.parseInt(a);
for (int i = 1; i <= itr; i++) {
multi m = new multi();
JLabel l1 = new JLabel("MANUEVER DETAILS");
JTextArea p = new JTextArea(
"Enter the manuever here");
panel1.add(l1);
panel1.add(p);
}
} catch (InvocationTargetException
| InterruptedException
| MatlabConnectionException
| MatlabInvocationException e) {
// TODO Auto-generated catch block
((Throwable) e).printStackTrace();
}
}
});
}
});
}
public static void main(String[] args) throws MatlabConnectionException,
MatlabInvocationException, IOException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
System.out.println("hey");
new BackgroundImageJFrame1();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
4 个解决方案
#1
5
you have a method in a method, put btnBrowseActionPerformed method outside of BackgroundImageJFrame1
方法中有一个方法,将btnBrowseActionPerformed方法放在BackgroundImageJFrame1之外
#2
2
You can not declare method inside method.
你不能在方法里面声明方法。
Take out btnBrowseActionPerformed
method outside BackgroundImageJFrame1()
constructor.
在BackgroundImageJFrame1()构造函数之外取出btnBrowseActionPerformed执行的方法。
This code is written inside method.
此代码是在方法内部编写的。
jb1.addActionListener(new java.awt.event.ActionListener() {
and this is a new method which should not be inside any method.
这是一种不应该在任何方法内的新方法。
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
Note: As mentioned by @Ranjeet, you need to declare
class level variables outside constructor, you can initialize inside constructor.
注意:正如@Ranjeet所提到的,您需要在构造函数外部声明类级变量,您可以初始化构造函数。
#3
0
- You need to write implementation of btnBrowseActionPerformed method outside constructor.
- 您需要在构造函数之外编写btnBrowseActionPerformed方法的实现。
- Declare final JFileChooser fc = null; final JTextField textField;at class level not in constructor.
- 声明最终的JFileChooser fc = null;最后的JTextField textField;在类级而不是在构造函数中。
- It resolve your compilation error problem.
- 它解决了编译错误问题。
#4
0
In the code that was posted, the anonymous class definition ended after the actionPerformed() method using the symbols });
在所发布的代码中,匿名类定义在actionPerformed()方法使用symbol}之后结束;
The btnBrowseActionPerformed() method is a private method of the same class and the class definition should include that method. Basically the }); that closes the anonymous class should be after the btnBrowseActionPerformed() definition.
btnBrowseActionPerformed()方法是同一个类的私有方法,类定义应该包含该方法。基本上});关闭匿名类应该在btnBrowseActionPerformed()定义之后。
jb1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBrowseActionPerformed(null);
}
// do not close the anonymous inner class here
//the below method belongs to the anonymous class.
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
if (fc == null) {
fc = new JFileChooser(".");
}
// Show it.
int returnVal = fc.showOpenDialog(null);
// Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
textField.setText(fc.getSelectedFile().getPath());
} else {
textField.setText("");
}
// Reset the file chooser for the next time it's shown.
fc.setSelectedFile(null);
}
});//close it here instead
#1
5
you have a method in a method, put btnBrowseActionPerformed method outside of BackgroundImageJFrame1
方法中有一个方法,将btnBrowseActionPerformed方法放在BackgroundImageJFrame1之外
#2
2
You can not declare method inside method.
你不能在方法里面声明方法。
Take out btnBrowseActionPerformed
method outside BackgroundImageJFrame1()
constructor.
在BackgroundImageJFrame1()构造函数之外取出btnBrowseActionPerformed执行的方法。
This code is written inside method.
此代码是在方法内部编写的。
jb1.addActionListener(new java.awt.event.ActionListener() {
and this is a new method which should not be inside any method.
这是一种不应该在任何方法内的新方法。
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
Note: As mentioned by @Ranjeet, you need to declare
class level variables outside constructor, you can initialize inside constructor.
注意:正如@Ranjeet所提到的,您需要在构造函数外部声明类级变量,您可以初始化构造函数。
#3
0
- You need to write implementation of btnBrowseActionPerformed method outside constructor.
- 您需要在构造函数之外编写btnBrowseActionPerformed方法的实现。
- Declare final JFileChooser fc = null; final JTextField textField;at class level not in constructor.
- 声明最终的JFileChooser fc = null;最后的JTextField textField;在类级而不是在构造函数中。
- It resolve your compilation error problem.
- 它解决了编译错误问题。
#4
0
In the code that was posted, the anonymous class definition ended after the actionPerformed() method using the symbols });
在所发布的代码中,匿名类定义在actionPerformed()方法使用symbol}之后结束;
The btnBrowseActionPerformed() method is a private method of the same class and the class definition should include that method. Basically the }); that closes the anonymous class should be after the btnBrowseActionPerformed() definition.
btnBrowseActionPerformed()方法是同一个类的私有方法,类定义应该包含该方法。基本上});关闭匿名类应该在btnBrowseActionPerformed()定义之后。
jb1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBrowseActionPerformed(null);
}
// do not close the anonymous inner class here
//the below method belongs to the anonymous class.
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
if (fc == null) {
fc = new JFileChooser(".");
}
// Show it.
int returnVal = fc.showOpenDialog(null);
// Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
textField.setText(fc.getSelectedFile().getPath());
} else {
textField.setText("");
}
// Reset the file chooser for the next time it's shown.
fc.setSelectedFile(null);
}
});//close it here instead