【问题标题】:Eclipse says I have a compilation error when I don't - How to fix?Eclipse 说我没有编译错误 - 如何修复?
【发布时间】:2016-07-11 08:59:45
【问题描述】:

我正在编写一个 java 项目,突然,eclipse 突然报告我的代码有问题。它特定于一个文件,但其他文件失败了,因为他们使用了相关类中的几个函数,他们似乎无法找到这些函数。我已经尝试了几乎所有我能想到的方法来消除这个错误。

  • 刷新项目
  • 清理项目
  • 删除项目并重新导入
  • 删除文件,新建,粘贴内容
  • 删除错误并清理项目

具体来说第一个错误是:

Syntax error, insert "}" to complete ClassBody

只是为了说明,这里是代码:

package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import model.HolderCompany;
import model.Row;
import viewmodel.App;

public class PanelMain extends JPanel {

    private PanelTable table;
    private DataDialog editDialog;
    private DataDialog addDialog;
    private ManageCompaniesDialog mcDialog;
    private PanelConsole consolePane;
    private JFrame frame;
    private ArrayList<Row> data;
    private ArrayList<HolderCompany> companies;

    private static String[] labelHeaders = {
        "ID",
        "Deployment Date",
        "IMEI",
        "Name",
        "Model",
        "Software Version",
        "A51 Device",
        "Holder Company",
        "Company E-mail",
        "Company Phone"
    }; //Here is the so called Syntax Error

    public PanelMain(){
        frame = new JFrame("Mobile Sensor Manager");

        setLayout(new BorderLayout());
        JPanel pane = new JPanel(new BorderLayout());

        consolePane = new PanelConsole();

        //Button Pane
        JButton addRow = new JButton("Add Row");
        addRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addDialog = openDialog("Add Row");
                //Wait for return here
                Object[] data = addDialog.getData();
                if(data != null){
                    table.addRowToTable(data);
                    consolePane.write("Added row: " + table.dataToString(data), null);
                }
            }
        });

        JButton editRow = new JButton("Edit Row");
        editRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = table.getSelectedRow();
                if(index == -1){
                    //Write error message
                    consolePane.write("No row is selected!", null);
                } else {
                    Object[] d1 = table.getData(index);
                    editDialog = openDialog("Edit Row", data);
                    Row data =     App.getSharedResources().getData().get(index); 
                    editDialog = openDialog("Edit Row", data);
                    //Wait for return here
                    Object[] newData = editDialog.getData();
                    Row newRow = editDialog.getRow();
                    if(newData != null){
                        table.changeRowInTable(index, newData);
                        App.getSharedResources().changeRow(index, newRow);
                        consolePane.write("Changed row " + index + " to " + table.dataToString(newData), null);
                    }
                }
            }
        });

        JButton deleteRow = new JButton("Delete Row");
        deleteRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = table.getSelectedRow();
                if(index == -1){
                    //Write error message
                    consolePane.write("No row is selected!", null);
                } else {
                    consolePane.write("Removed row: " + table.tableIndexToString(index), null);
                    table.deleteRowFromTable(index);
                    App.getSharedResources().removeRow(index);
                }
            }
        });

        JButton manageCompanies = new JButton("Manage Companies");
        manageCompanies.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                mcDialog = openDialog();
            }
        });

        JButton clearDB = new JButton("Clear Database");
        JButton deleteDB = new JButton("Delete Database");

        JPanel buttonPane = new JPanel(new GridLayout(0, 1));
        TitledBorder buttonBorder = new TitledBorder("Buttons");
        buttonPane.setBorder(buttonBorder);

        buttonPane.add(addRow);
        buttonPane.add(editRow);
        buttonPane.add(deleteRow);
        buttonPane.add(new JSeparator());
        buttonPane.add(manageCompanies);
        buttonPane.add(new JSeparator());
        buttonPane.add(clearDB);
        buttonPane.add(deleteDB);

        //Table Pane
        table = new PanelTable();

        JPanel tablePane = new JPanel(new BorderLayout());
        TitledBorder dbContentBorder = new TitledBorder("Database Content");
        tablePane.setBorder(dbContentBorder);
        tablePane.add(table, BorderLayout.CENTER);

        pane.add(buttonPane, BorderLayout.LINE_END);
        pane.add(tablePane, BorderLayout.CENTER);
        pane.add(consolePane, BorderLayout.PAGE_END);

        add(pane, BorderLayout.CENTER);
    }

    public DataDialog openDialog(String name){
        Window win = SwingUtilities.getWindowAncestor(this);
        return new DataDialog(win, new DataTemplate(), name);
    }

    public DataDialog openDialog(String name, Object[] data){
        Window win = SwingUtilities.getWindowAncestor(this);
        return new DataDialog(win, new DataTemplate(), name, data);
    }

    public DataDialog openDialog(String name, Row data){
        Window win = SwingUtilities.getWindowAncestor(this);
        return new DataDialog(win, new DataTemplate(), name, data);
    }

    public ManageCompaniesDialog openDialog() {
        Window win = SwingUtilities.getWindowAncestor(this);
        return new ManageCompaniesDialog(win, new     ManageCompaniesTemplate(companies));
    }

    public void injectCompanies(ArrayList<HolderCompany> companies) {
        this.companies = companies;
    }

    public void injectData(ArrayList<Row> data) {
        this.data = data;
        table.injectDataToTable(data);
    }

    public void addMainToFrame(PanelMain main) {
        frame.add(main);
    }

    public void createAndShowGUI(){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    public static String[] getHeaders() {
        return labelHeaders;
    }
}

编辑

是只有我还是有其他人收到此错误?

欢迎任何关于如何修复它的建议!

【问题讨论】:

  • 能否指定*行号*
  • @MehrajMalik:更有用的是,他在代码中注释了这行代码。
  • 我在代码中发布了一条关于错误出现位置的评论。但它在第 42 行
  • 是 Eclipse 工作区元数据问题吗?如果您创建一个新工作区并导入项目会发生什么?
  • 同样的事情。我刚刚创建了一个全新的工作区并导入了项目,但仍然出现错误。

标签: java eclipse compiler-errors


【解决方案1】:

好的。我终于设法修复了错误。

如顶帖中的 cmets 所述,如果我清除构造函数,我可以编译它。一旦我得到它编译,我慢慢地将代码逐行复制粘贴到我的项目中。我省略了 ActionListeners,它编译得很好。在这之后我慢慢地一个一个地导入ActionListener方法,我找到了导致错误的一行。是这样的:

App.getSharedResources().changeRow(index, newRow); //line 106

这个方法还没有实现,在上面解释的过程之后它给了我一个错误。我可以创建该方法,并且我的其余代码现在可以按预期编译。这是一个非常奇怪的错误。

【讨论】:

  • 感谢分享。 (顺便说一句:考虑接受您自己的答案以标记它解决了所描述的问题)。
【解决方案2】:

我过去也遇到过类似的问题,请您尝试从“问题”视图中删除错误,然后执行一个干净的项目,看看它是否消失。

如果没有帮助,请尝试:查看 .log 在您的工作区目录中创建的 .log 文件。这可能是如何解决这个问题的线索。

【讨论】:

  • 我已经尝试过了。它没有用。但是,我确实将我的问题设置为答案。不过感谢您的输入;)
猜你喜欢
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2015-07-29
相关资源
最近更新 更多