找不到“class,interface或enum expected”错误的来源

时间:2014-09-16 17:14:25

标签: java enums

在尝试构建此项目时,我仍然收到错误class, interface, or enum expected。我已经把事情搞得一团糟,但我似乎无法找出问题所在。请问有人帮忙吗?

控制台中显示的错误是:

ant -f C:\\Users\\x\\Documents\\NetBeansProjects\\DisplayDataUsingJTable -Dnb.internal.action.name=rebuild clean jar
init:
deps-clean:
Updating property file: C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build\built-clean.properties
Deleting directory C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build
clean:
init:
deps-jar:
Created dir: C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build
Updating property file: C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build\built-jar.properties
Created dir: C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build\classes
Created dir: C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build\empty
Created dir: C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build\generated-sources\ap-source-output
Compiling 3 source files to C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\build\classes
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:245: error: class, interface, or enum expected
    private void initComponents() {
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:248: error: class, interface, or enum expected
        setFocusable(false);
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:250: error: class, interface, or enum expected
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
C:\Users\Imtiaaz\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:251: error: class, interface, or enum expected
        getContentPane().setLayout(layout);
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:252: error: class, interface, or enum expected
        layout.setHorizontalGroup(
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:256: error: class, interface, or enum expected
        layout.setVerticalGroup(
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:261: error: class, interface, or enum expected
        pack();
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\src\pack1\DisplayPerson.java:262: error: class, interface, or enum expected
    }// </editor-fold>//GEN-END:initComponents
8 errors
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\nbproject\build-impl.xml:924: The following error occurred while executing this line:
C:\Users\x\Documents\NetBeansProjects\DisplayDataUsingJTable\nbproject\build-impl.xml:264: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

错误行来到这里:

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setFocusable(false);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 1535, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 755, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold> 

下面的完整代码 - 不包括sql语句:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package pack1;
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class DisplayPerson extends javax.swing.JFrame {
    String driverName = "net.sourceforge.jtds.jdbc.Driver";
    String serverName = "xxxxx";
    String serverPort = "xxxx";
    String database = serverName + ":" + serverPort;
    String url = "jdbc:jtds:sqlserver:/" + database;
    String username = "xx";
    String password = "xxx";

    public DisplayPerson() throws SQLException {
        ArrayList columnNames = new ArrayList();
        ArrayList data = new ArrayList();

        try {
            Class.forName(driverName);
            Connection connection = DriverManager.getConnection(url, username, password);

            // Create and execute an SQL statement that returns some data.
            String SQL = "xxxxx";
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            ResultSetMetaData  rsmetadata = rs.getMetaData();

            int columns = rsmetadata.getColumnCount();

            //  Get column names
            for (int i = 1; i <= columns; i++) {
                columnNames.add( rsmetadata.getColumnName(i) );
            }

            //  Get row data
            while (rs.next()) {
                ArrayList row = new ArrayList(columns);

                for (int i = 1; i <= columns; i++) {
                    row.add( rs.getObject(i) );
                }

                data.add( row );
            }
        }
        catch (SQLException e) {
            System.out.println( e.getMessage() );
        }
        catch (ClassNotFoundException ex) {
            Logger.getLogger(DisplayPerson.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Create Vectors and copy over elements from ArrayLists to them
        // Vector is deprecated but I am using them in this example to keep 
        // things simple - the best practice would be to create a custom defined
        // class which inherits from the AbstractTableModel class
        Vector columnNamesVector = new Vector();
        Vector dataVector = new Vector();

        for (int i = 0; i < data.size(); i++) {
            ArrayList subArray = (ArrayList)data.get(i);
            Vector subVector = new Vector();
            for (int j = 0; j < subArray.size(); j++) {
                subVector.add(subArray.get(j));
            }
            dataVector.add(subVector);
        }

        for (int i = 0; i < columnNames.size(); i++)
            columnNamesVector.add(columnNames.get(i));

        //  Create table with database data    
        JTable table;
        table = new JTable(dataVector, columnNamesVector) {

            public Class getColumnClass(int column) {
                for (int row = 0; row < getRowCount(); row++) {
                    Object o = getValueAt(row, column);

                    if (o != null) {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

        JPanel buttonPanel = new JPanel();
        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

    public static void main(String args[]) throws SQLException {
        DisplayPerson frame = new DisplayPerson();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setFocusable(false);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 1535, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 755, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
* @param args the command line arguments
*/

    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */

    //</editor-fold>

    /* Create and display the form */
    //java.awt.EventQueue.invokeLater(new Runnable() {

// Variables declaration - do not modify
// End of variables declaration

2 个答案:

答案 0 :(得分:0)

不确定您是否观察到它,但您的initComponents()方法不在DisplayPerson类的右大括号中。将它移到课堂内并且该特定错误消失

答案 1 :(得分:0)

我和Netbeans有同样的问题,结果是错误的关闭&#39;}&#39;}在主班的中间。通过高举开幕式找到它&#39; {&#39;在课堂声明和向下滚动,找到匹配的&#39;}&#39;那不属于。