Java:此行的多个标记 - 令牌上的语法错误“)”,; expected - 令牌上的语法错误“(”,{预期

时间:2015-04-16 11:18:12

标签: java syntax-error

我无法找到错误。 Eclipse给了我这个错误。每个{ }都匹配。请帮忙。

  

此行的多个标记 - 令牌上的语法错误“)”,;预期    - 令牌上的语法错误“(”,{expected

我的代码如下:

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 个答案:

答案 0 :(得分:4)

你在方法中有一个方法,把btnBrowseActionPerformed方法放在BackgroundImageJFrame1之外

答案 1 :(得分:2)

你不能在方法内部声明方法。

btnBrowseActionPerformed构造函数之外取出BackgroundImageJFrame1()方法。


此代码写在方法内。

jb1.addActionListener(new java.awt.event.ActionListener() {

这是一个新方法,不应该在任何方法中。

private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {

注意:正如@Ranjeet所提到的,你需要在构造函数之外的declare类级变量,你可以初始化构造函数内部。

答案 2 :(得分:0)

在发布的代码中,匿名类定义在使用符号})的actionPerformed()方法之后结束;

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 

答案 3 :(得分:0)

  • 您需要在构造函数外部编写btnBrowseActionPerformed方法的实现。
  • 声明final JFileChooser fc = null; final JTextField textField;在类级别而不是在构造函数中。
  • 它解决了编译错误问题。