我正在编写一个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;
}
}
只是我或其他人是否收到此错误?
欢迎任何有关如何修复它的建议!
答案 0 :(得分:2)
好。我终于设法解决了这个错误。
正如在顶部帖子中的评论中所提到的,如果我清除了构造函数,我可以将其编译。一旦我得到它编译我慢慢地将代码逐行粘贴到我的项目中。我省略了ActionListeners,它编译得很好。在此之后,我慢慢地逐个导入ActionListener方法,我发现了一行导致错误。这是一行:
App.getSharedResources().changeRow(index, newRow); //line 106
这个方法还没有实现,它在上面说明的过程之后给了我一个错误。我可以创建该方法,我的其余代码现在按预期编译。这是一个非常奇怪的错误。
答案 1 :(得分:1)
过去某个时候我也遇到过类似的问题,请你试着从“问题”中删除错误。之后查看并执行一个干净的项目,看它是否消失。
如果没有帮助尝试:查看工作区目录中eclipse创建的.log文件。这可能会为如何解决这个问题提供线索。