从终端编译时出现 IntelliJ 错误 "contentPane cannot be set to null." 的 Swing GUI

Swing GUI with IntelliJ error "contentPane cannot be set to null." when compiling from terminal

当我从 IntelliJ 编译我的项目时没有任何问题并且我的程序运行顺利,但是当我尝试使用 javac 从终端编译它时,警告 "Note: Victor/presentation/TableControllerMenu.java uses unchecked or unsafe operations." 弹出,当我尝试执行它我收到错误 "Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null."。我猜 IntelliJ 没有在文件中编写对象 contentPane 的初始化代码,这导致 javac 崩溃。 我试图将选项文件 -> 设置 -> GUI 设计器 -> 将 GUI 生成从 "Binary source file" 更改为 "Java source code",但在编译时产生了更多错误...

这是 GUI 的代码,如果需要更多代码请告诉我:

package Victor.presentation;

import Victor.domain.TCPresPersInterface;
import Victor.persistence.TCRow;
import Victor.persistence.TCUtilities;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class TableControllerMenu extends JFrame {
    static private TCPresPersInterface persInterface;
    private DefaultTableModel newModel;
    private JTableHeader newHeader;

    private JPanel mainPanel;
    private JButton rowEditorButton;
    private JButton columnCastingButton;
    private JButton manageTablesButton;
    private JButton columnEditorButton;
    private JButton basicUtilitiesButton;
    private JButton fileHandlerButton;
    private JButton goBackButton;
    private JPanel ManageTablesPanel;
    private JPanel BasicUtilitiesPanel;
    private JPanel RowEditorPanel;
    private JPanel ColumnEditorPanel;
    private JPanel ColumnCastingPanel;
    private JPanel FileHandlerPanel;
    private JButton duplicateCurrentTableButton;
    private JButton renameCurrentTableButton;
    private JTextField duplicateTableNameField;
    private JTextField renameTableField;
    private JButton renameColumnButton;
    private JButton modifyItemButton;
    private JButton clearTableButton;
    private JTextField newColumnNameField;
    private JTextField newItemField;
    private JTextField selectedColumnRenameField;
    private JTextField newItemColumnField;
    private JTextField newItemRowField;
    private JCheckBox clearTableConfirmationBox;
    private JButton addEmptyRowButton;
    private JButton duplicateRowButton;
    private JButton deleteRowButton;
    private JTextField emptyRowField;
    private JTextField rowDuplicateField;
    private JTextField positionDuplicatedField;
    private JTextField rowDeleteFIeld;
    private JButton addEmptyColumnButton;
    private JButton duplicateColumnButton;
    private JButton moveColumnButton;
    private JButton negateBinaryColumnButton;
    private JButton deleteColumnButton;
    private JTextField addEmptyColPosFIeld;
    private JTextField duplicateColColFIeld;
    private JTextField duplicatedColPosField;
    private JTextField moveColColFIeld;
    private JTextField moveColPosFIeld;
    private JTextField negateColColField;
    private JTextField deleteColColField;
    private JButton columnToNumericalButton;
    private JButton columnToBinaryButton;
    private JButton columnToBinaryByButton;
    private JButton columnToCategoricalButton;
    private JTextField colNumericalColField;
    private JTextField colBinaryColField;
    private JComboBox colBinItemsBox;
    private JTextField colBinIntervalDivField;
    private JComboBox colBinaryIntervalLowerBox;
    private JComboBox colBinEqualBox;
    private JTextField colCategoricalField;
    private JComboBox selItemsValBox;
    private JTextField colBinIntervalColField;
    private JButton importCSVButton;
    private JButton exportCSVButton;
    private JTextField importPathField;
    private JTextField pathExportField;
    private JTextField separatorImportField;
    private JComboBox headerImportField;
    private JComboBox dataTypeImportBox;
    private JTextField separatorExportField;
    private JComboBox headerExportField;
    private JComboBox dataTypeExportField;
    private JTabbedPane currentMenuTab;
    private JPanel currentMenuPane;
    private JTabbedPane tabbedPane1;
    private JPanel currentTablePanel;
    private JTable currentTable;
    private JComboBox newColumnDType;
    private JLabel manageTablesErrorLog;
    private JLabel currentTableText;
    private JLabel tableSizeText;
    private JLabel errorLogText;
    private JLabel tableHeaderText;
    private JLabel tableTypesText;
    private JTextField separatorColBinFIeld;
    private JTextField itemsColBinFIeld;
    private JButton deleteItemBoxButton;

    private TableControllerMenu(String appName) {
        super(appName);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(mainPanel);
        this.pack();

        persInterface = TCPresPersInterface.getInstance();

        actualize(true);

        //      DUPLICATE CURRENT TABLE BUTTON
        duplicateCurrentTableButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String newName = duplicateTableNameField.getText();

                String error = persInterface.duplicateTable(newName);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    duplicateTableNameField.setText("");
                }
            }
        });

        //      RENAME CURRENT TABLE BUTTON
        renameCurrentTableButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                String newName = renameTableField.getText();

                String error = persInterface.renameTable(newName);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(false);
                    renameTableField.setText("");
                }
            }
        });

        //      IMPORT TABLE FROM FILE BUTTON
        importCSVButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String path = importPathField.getText();
                String separator = separatorImportField.getText();
                String header = (String) headerImportField.getSelectedItem();
                String type = (String) dataTypeImportBox.getSelectedItem();

                String error = persInterface.fileHandling(false, path, separator, header, type);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    importPathField.setText("");
                    separatorImportField.setText("");
                    headerImportField.setSelectedIndex(0);
                    dataTypeImportBox.setSelectedIndex(0);
                }
            }
        });

        //      EXPORT TABLE TO FIELD BUTTON
        exportCSVButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String path = pathExportField.getText();
                String separator = separatorExportField.getText();
                String header = (String) headerExportField.getSelectedItem();
                String type = (String) dataTypeExportField.getSelectedItem();

                String error = persInterface.fileHandling(true, path, separator, header, type);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(false);
                    pathExportField.setText("");
                    separatorExportField.setText("");
                    headerExportField.setSelectedIndex(0);
                    dataTypeExportField.setSelectedIndex(0);
                }
            }
        });

        //      RENAME COLUMN BUTTON
        renameColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String newName = newColumnNameField.getText();
                String col = selectedColumnRenameField.getText();

                String error = persInterface.renameColumn(newName, col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    newColumnNameField.setText("");
                    selectedColumnRenameField.setText("");
                }
            }
        });

        //      MODIFY ITEM BUTTON
        modifyItemButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String value = newItemField.getText();
                String row = newItemRowField.getText();
                String col = newItemColumnField.getText();

                String error = persInterface.modifyItem(value, row, col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    newItemField.setText("");
                    newItemRowField.setText("");
                    newItemColumnField.setText("");
                }
            }
        });

        //      CLEAR TABLE BUTTON
        clearTableButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (clearTableConfirmationBox.isSelected()) {
                    String error = persInterface.clearTable();

                    if (!error.equals(""))
                        errorLogText.setText("ERROR: " + error);
                    else {
                        errorLogText.setText("");
                        actualize(true);
                        clearTableConfirmationBox.setSelected(false);
                    }
                } else
                    errorLogText.setText("ERROR: the 'are you sure' box must be marked in order to clear the table");
            }
        });

        //      ADD EMPTY ROW BUTTON
        addEmptyRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String position = emptyRowField.getText();

                String error = persInterface.addEmptyRow(position);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    emptyRowField.setText("");
                }
            }
        });

        //      DUPLICATE ROW BUTTON
        duplicateRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String row = rowDuplicateField.getText();
                String pos = positionDuplicatedField.getText();

                String error = persInterface.duplicateRow(row, pos);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    rowDuplicateField.setText("");
                    positionDuplicatedField.setText("");
                }
            }
        });

        //      DELETE ROW BUTTON
        deleteRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String row = rowDeleteFIeld.getText();

                String error = persInterface.deleteRow(row);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    rowDeleteFIeld.setText("");
                }
            }
        });

        //      ADD EMPTY COLUMN BUTTON
        addEmptyColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String pos = addEmptyColPosFIeld.getText();
                String dtype = (String) newColumnDType.getSelectedItem();

                String error = persInterface.addEmptyColumn(pos, dtype);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    addEmptyColPosFIeld.setText("");
                    newColumnDType.setSelectedIndex(0);
                }
            }
        });

        //      DUPLICATE COLUMN BUTTON
        duplicateColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = duplicateColColFIeld.getText();
                String pos = duplicatedColPosField.getText();

                String error = persInterface.duplicateCol(col, pos);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    duplicateColColFIeld.setText("");
                    duplicatedColPosField.setText("");
                }
            }
        });

        //      MOVE COLUMN BUTTON
        moveColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = moveColColFIeld.getText();
                String pos = moveColPosFIeld.getText();

                String error = persInterface.moveCol(col, pos);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    moveColColFIeld.setText("");
                    moveColPosFIeld.setText("");
                }
            }
        });

        //      NEGATE BINARY COLUMN BUTTON
        negateBinaryColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = negateColColField.getText();

                String error = persInterface.negateBinaryCol(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    negateColColField.setText("");
                }
            }
        });

        //      DELETE COLUMN BUTTON
        deleteColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = deleteColColField.getText();

                String error = persInterface.deleteCol(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    deleteColColField.setText("");
                }
            }
        });

        //      COLUMN TO NUMERICAL BUTTON
        columnToNumericalButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = colNumericalColField.getText();

                String error = persInterface.colToNumeric(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    colNumericalColField.setText("");
                }
            }
        });

        //      COLUMN TO BINARY BUTTON
        columnToBinaryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String items = itemsColBinFIeld.getText();
                String separator = separatorColBinFIeld.getText();
                String castTo = (String) selItemsValBox.getSelectedItem();
                String col = colBinaryColField.getText();

                String error = persInterface.colToBinary(col, items, separator, castTo);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    selItemsValBox.setSelectedIndex(0);
                    colBinaryColField.setText("");
                    itemsColBinFIeld.setText("");
                    separatorColBinFIeld.setText("");
                }
            }
        });

        //      COL TO BINARY INTERVAL
        columnToBinaryByButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String div = colBinIntervalDivField.getText();
                String lower = (String) colBinaryIntervalLowerBox.getSelectedItem();
                String equal = (String) colBinEqualBox.getSelectedItem();
                String col = colBinIntervalColField.getText();

                String error = persInterface.colToBinaryInterval(col, div, lower, equal);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    colBinIntervalDivField.setText("");
                    colBinaryIntervalLowerBox.setSelectedIndex(0);
                    colBinEqualBox.setSelectedIndex(0);
                    colBinIntervalColField.setText("");
                }
            }
        });

        //      COLUMN TO CATEGORICAL BUTTON
        columnToCategoricalButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = colCategoricalField.getText();

                String error = persInterface.colToCategory(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    colCategoricalField.setText("");
                }
            }
        });
    }

    private void actualize(Boolean reloadTable) {
        String current = persInterface.getCurrentTable();
        currentTableText.setText("Current table: " + current);
        tableSizeText.setText("Size: " + persInterface.getSizeText());
        errorLogText.setText("");

        if (reloadTable) {
            try {
                //TCUtilities.print(current);

                int n_cols = TCUtilities.getNCols(current);
                int n_rows = TCUtilities.getNRows(current);

                String header_string = Arrays.toString(TCUtilities.getHeader(current).toArray());
                header_string = header_string.replace("[", "");
                header_string = header_string.replace("]", "");
                String type_string = "";
                for (int i = 0; i < n_cols; i++) {
                    if (i != 0) type_string += ", ";
                    type_string += TCUtilities.getColType(current, i).toString();
                }

                tableHeaderText.setText("Header: " + header_string);
                tableTypesText.setText("Column types: " + type_string);

                newModel = new DefaultTableModel(n_cols, n_rows + 2);

                for (int i = 0; i < TCUtilities.getNRows(current); i++) {
                    newModel.insertRow(i, (Object[]) TCRow.getRow(current, i).toArray(new Object[n_cols]));
                }

                currentTable.setModel(newModel);

                newHeader = new JTableHeader();

            } catch (Exception e) {
                System.out.println("ERROR IN ACTUALIZE: " + e.getMessage());
            }
        }
    }

    public static String execute(String currentTable) {
        TCPresPersInterface.setCurrentTable(currentTable);

        JFrame frame = new TableControllerMenu("Table Managing and Editing");
        frame.setVisible(true);

        return TCPresPersInterface.getCurrentTable();
    }

    public static void setCurrentTable(String currentTable) {
        persInterface.setCurrentTable(currentTable);
    }

    public static Boolean hasCurrentTable() {
        return persInterface.hasCurrentTable();
    }

}

我终于解决了。我所做的是:文件 -> 设置 -> 编辑器 -> GUI 设计器并将选项 "Generate GUI into:" 从二进制 class 文件更改为 Java 源代码并在所有窗格中更改 'Layout manager' 选项 grom Grid Layout Manager (IntelliJ) 到 GridBagLayout(我想这是可行的,因为它不是内部 IntelliJ 格式),并且它已编译。